|
Since I have started developing my ‘GD Pages Navigator’ widget plugin, I wanted to upgrade it so it can support more then one instance on a page. Unfortunatly there are limited resources available for that task, and almost no info about it in the official documentation. There are plugins that use different methods of achiving this, but best method is the one used by plugins build into Wordpress. So, my starting point was ‘widget.php’ file where this default widgets are located. They also provided a short example of multi instance widget at the end of this file. This example in is good, but it has few errors (copy-paste errors mostly). This function is in the sample code class I have decided to walk you throught the process of creating a widget that will support more then one instance. This example will show you how to work with different types of settings and it will provide you with some practicle code snippets you can use in your own plugins. Tutorial will be based on my ‘GD Pages Navigator’ plugin. This widget supports multiple instances from the version 3.0.0. Wordpress Widgets PageThis page is used to handle setting widgets on sidebars. Once loaded this page shows all available widgets and widgets used by the currently active sidebar. When you save changes you made, complete page contents is posted back to the server. Then ‘widegt.php’ is called twice: first to process posted data, second is 302 redirect to display page to the user and show update message. Each form element for your widget control should have unique name, but for multi instance support you need to make this a bit different. Wordpress will generate unique ID’s for each instance of widget used. Basic Widget StructureFirst, in Wordpress plugins folder create folder ‘gd-multi’. This will be folder for your plugin. Create 3 files there:
Code bellow represents basic widget class that goes into ‘gd-multi.php’ file:
If you don’t need CSS or JS, you can remove action from constructor, and ‘admin_head()’ method. Init() method will be used to initialize one or more instances of the widget. Widget() method is actual rendering of the widget contents on the blog pages sidebar(s). Control() method will contain code for displaying and handling adding and settings of the widget. Admin_head() method is used to add stylesheet and jasvascript to be loaded on administration pages, so you can ues them in the widget control dialog. Storing Widget SettingsMain problem with multi instance widgets is saving their settings. You need to have a way of storing individual settings for each widget instance. The best way is to use an array based on widget id, and for each widget instance you have one array element. This one element can also be a array because you will usually have more then one setting you need to store for the widget. Our sample widget will have 3 options we will need to save:
We will need default settings stored, beacuse that will make it easier to handle later. So single widget settings array will look like this:
All settings will ne stored in the another array, where each element of the array will be array like one we have created above. Initializing WidgetWidget init code is used to register one or more instances of the widget plugin depending how many of them you want to use. To do this we will use this complet code for init() method:
On lines 5 and 6 we are setting options needed for registering widget, and we use them in lines 16, 17 and 20, 21. Variable $o is widget id that will be set by wordpress once we add widget to the sidebar. If no widgets are registered, then default id will be 1. Wordpress register sidebar method will call method widget(), and registering control will use control() method. Widget ControlThe code that follows goes into control() method. First we need to get widget number and initialize options if they are not initialized already. Similar code will be used in the widget rendering method.
And now to the most important part of the widget control. This part will be executed only after user saves sidebar widgets. We will then search through widgets registered in sidebar to get all instances of our plugin. If we found instance in the sidebar, but not in the post, we will remove that instance from the sidebar. After that we go thorught the post elements of our widget, and save them into an array, and update widget options. Complete code for this is in the next block.
After that we need to display control form for the widget. If the widget is new instance without wisget id we will use default settings, if not, then settings will be read from saved options. After that we show the form. Form goes into sepparate file ‘gd-multi-form.php’.
If you wonder what is ‘%i%’, don’t wonder any more. This value will be replaced by Wordpress javascript code with real generated widget id once we add the widget into the sidebar. Widget Control FormFor the form elements you can use anything for id attribute, but name attribute must be in the format specified in the the example. That way once we get posted data, we can access them as they were in array based on the widget name, widget id and actual values. For a pages selection list we use an additional function ‘render_options()’ in the main class for rendering pages options. Each form element has a name attribute in the following form: Not to waste too much space in the article, complete form (it’s really simple as you can see on the picture) is in the sample download file. Widget OutputFinally, we need to display our widget(s) in the sidebars for your visitors to see. This code is all in widget(). Each instance of the widget has it’s own settings in main otpions array. First we need to get that individual instance array. For this we can use the same code as in the control method. After that we use one new method to render pages according to selection. Download source codeI have compiled the files we have created and changed in this article, so you can see complete code you can test and play with.
ConclusionSo, I hope you find this article useful. If you have any suggestion on improving it, or make some change if you notice some errors, please leave a comment. |
Comments:
Where can I download sourcecode
Link was lost somehow, I have added link again in the post.
Post a Comment
You must be logged in to post a comment.