Multi Widgets
Creating Multi Widgets just got easier.
There are some good sites which explain this with some example code.
Some good examples are available at: http://wpengineer.com/wordpress-built-a-widget/
If you do want to try out the example on that page you’ll need to do a couple of things.
- Create a new folder under your ../wp-content/plugins .
- Create a new file to save it in.
- Add the Plugin Header – The example on the page doesn’t show one – it’s an example!
- Add a registration function to keep things happy. What is there won’t work – or it didn’t work for me so needed this addition.
Now this example code worked after I performed the changes above ( Yes I am going to describe them in a moment!) . As it is an example – you are going to have to make some decisions on what you are going to call it etc.
Step 1.
Now as this is an example of a Multi Widget – I created a folder called multi-1 ( when I’m experimenting/Developing – I always put my plug-in code in Folders ).
Step 2.
Next is to copy and paste the sample code into a file – again I called this multi-1.php and this lives in the previously created folder with the same name.
Step 3.
Add in the following Plugin Header – Remember this is an example bit of code we are just seeing what it does…So some of the info doesn’t matter. The one that does is the Plugin Name.
Plugin Name: Multi -1 Plugin URI: http://www.somewhere.com Description: Example of a Multi Widget Version: 0.1 Author: Multi-1 Author URI: http://www.somewhere.com
The important line here is Plugin Name: Multi -1 as this is what you are going to see in your Plugin page – the plugin name called Mutli-1 – so you can find it to activate it. That always helps.
Step 4.
Add the additional registration code.
This is a snippet of the original code from the example.
?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p> <p><label for="<?php echo $this->get_field_id('entry_title'); ?>">Title for entry feed: <input id="<?php echo $this->get_field_id('entry_title'); ?>" name="<?php echo $this->get_field_name('entry_title'); ?>" type="text" value="<?php echo attribute_escape($entry_title); ?>" /></label></p> <p><label for="<?php echo $this->get_field_id('comments_title'); ?>">Title for comments feed: <input id="<?php echo $this->get_field_id('comments_title'); ?>" name="<?php echo $this->get_field_name('comments_title'); ?>" type="text" value="<?php echo attribute_escape($comments_title); ?>" /></label></p> <?php } } register_widget('My_RSS_Widget');
And this is the code with the added function. Again this is just one way to achieve this…
?> <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label> <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /> <label for="<?php echo $this->get_field_id('entry_title'); ?>">Title for entry feed:</label> <input id="<?php echo $this->get_field_id('entry_title'); ?>" name="<?php echo $this->get_field_name('entry_title'); ?>" type="text" value="<?php echo attribute_escape($entry_title); ?>" /> <label for="<?php echo $this->get_field_id('comments_title'); ?>">Title for comments feed:</label> <input id="<?php echo $this->get_field_id('comments_title'); ?>" name="<?php echo $this->get_field_name('comments_title'); ?>" type="text" value="<?php echo attribute_escape($comments_title); ?>" /> } } add_action('widgets_init', 'My_RSS_Widget_init'); function My_RSS_Widget_init () { register_widget('My_RSS_Widget'); }
The bit we changed is…
add_action('widgets_init', 'My_RSS_Widget_init'); function My_RSS_Widget_init () { register_widget('My_RSS_Widget'); }
So after you’ve done all that, you can go into your Plugins panel and activate the plugin called Multi-1 . Then go into Appearance -> Widgets and install a few instances of Multi-1.
One thing I did check was the table – wp-options – to see what the new entries were. This new wp_widget class takes care of the creating and destroying of each widgets values. Which if I were to do this the old way.. I’d have to write the code to achieve the same thing. This is where this Class really shines. So now I ( and you ) can get busy creating multi widgets and not worry about all the “housekeeping” code.
Just remember…
This new class only works with WordPress 2.8 and above. Can’t say it works in the upcoming WordPress 3 as it’s not around yet… Everyone should be running the latest versions of WordPress anyway so if they want to use your new Multi Widget – they’ll have no excuses not too!
Ok, I think that covers most of it.. If not, please leave a comment and I’ll get back to you on it.
What I have described here is exactly what I have done and it works a treat. So it’s a very good “framework” to build upon. Have fun!
December 31, 2009
|
Posted by Tim Brownlaw
Categories:
