Thursday, 21 March 2013

How to make a Worpress Widget Plugin - Stage 1

Here's what I want to do: Grab the latest picture from my account ( which is what I send to from my mobile Twitter account ) .
The way I'd like to do this in Wordpress is to create a plugin. To create a Wordpress Widget then we need to extend the WP_Widget API. Make sure you follow the example here .

Here's my notes on how I did it; that may help you and will hopefully help myself to simplify the process when I come back to it in future.

 1. Once the Class is set up the key areas to remember are (

 form() - this is whats called up in (Appearance -> Widgets)

 update() - saves the data weve entered above

 widget() - output the widget from the details we have above and what we've created in this class

The following code is to show the first stage of writing my plugin. I think it shows a good example of how the 'Sample Plugin' in the above link is expanded.

Notice how an extra instance is added to the form and how that is dealt with in 'update. The 'widget function' is at its most basic to prove we can save information and retrieve it.
 * Plugin Name: Image Displayer
 * Plugin URI:
 * Description: A widget that shows your latest Ow Dot image 
 * Version: 0.1
 * Author: DJ Millward
 * Author URI:
 * License: GPLv2 or later
 * Text Domain: owdotly-image
 class Owdotly_Image_Widget extends WP_Widget 
  public function __construct()
parent::__construct('Owdotly_Image_Widget',' Image Displayer Widget',
array('description' => __('This widget will show your latest image',''),));
  function form($instance) {     
    $instance = wp_parse_args((array) $instance, array( 'title' => '', 'twit_username' => ''));
    $title = $instance['title'];
	$twit_username = $instance['twit_username'];

"; echo "$User "; } } add_action( 'widgets_init', create_function('', 'return register_widget("Owdotly_Image_Widget");') );

No comments: