Many of the WordPress plugin authors don’t offer widgets, but only raw
PHP functions (or hooks) which you have to insert into the theme’s template files. As the number of functions increases it becomes harder to manage it all, especially if you decide to uninstall some of them or add new ones.
It is especially inconvenient for those who are not so savvy, or don’t want to edit theme files. Moreover, if one decides to change the theme, the edits have to be repeated all over again.
Therefore, I made this Custom Function Widget plugin which allows you to create sidebar widgets without ever touching the theme’s files and use these widgets in any theme you like.
This plugin offers you up to 20 widgets you can then add to your theme’s sidebars. All you have to know is:
- the name of the function, and
- the arguments it requires (if any).
Additionally you can wrap the output in
HTML, remove the widget’s title from the output, or remove the default widget wrapper (set in
functions.php of your theme).
Download Custom Function Widgets Plugin
Important: It has been tested to be compatible with WordPress 2.5 and 2.6.
Download custom-function-widgets.zip (Version 0.2 / 127 KB) — includes the plugin, readme.txt and four screenshots.
- Download the plugin and unzip its content
- Upload all of the
/wp-content/plugins/directory. The final directory tree would like
- Activate the plugin through the ‘Plugins’ menu in your WordPress dashboard.
- Go to ‘Presentation’ > ‘Widgets’ inside your WordPress dashboard. Just bellow the list of sidebars you will see a list of ‘Available Widgets’. There you will find five new widgets ‘Custom Function 1’, ‘Custom Function 2’, etc.
- Drag one of these widgets inside the sidebar of your choice. Once it is there, click on the widget’s options button (next to its title), which will open the widget’s options dialog box.
- There you can specify the following:
- Wiget title
- Name of the function you want to call (the only obligatory item you must specify)
- Arguments to pass to the function, like
$onearg = 2, 'Something', $other = 3
- HTML to display before the output of the function
- HTML to display after the output of the function
- Option to hide the title of the widget (which you specified as Widget title) during the ouput
- Option to remove the default wrapper of the widget (defined in
template.phpof your theme’s folder, for each sidebar)
- Close the widget options pop-up and click ‘Save Changes’
- Now you should see the new widget in the sidebar of you blog/site
Example: creating a widget for Popularity Contest plugin
To better illustrate where to find this information and how to create a widget, lets use the ‘Popularity Contest‘ plugin by Alex King. Once you install Popularity Contest, you have several PHP hooks available:
akpc_most_popular($limit = 10, $before = <li>, $after = </li>)
akpc_most_popular_in_cat($limit = 5, $before, $after, $cat_ID = current category)
akpc_most_popular_in_month($limit, $before, $after, $m = YYYYMM)
To use either of these hooks, simply drag an new ‘Custom Function’ widget (from ‘Available Widgets’ under ‘Presentation’ > ‘Widgets’) inside a sidebar of your choice.
Click on the Widget’s options button and enter the following details (for the
akpc_most_popular hook as an example):
- Widget title:
Most Popular Posts
- Function name:
- Function variables:
$limit = 5, $before = <li>, $after = </li>
- HTML Before:
- HTML After:
- Leave unchecked both
'Remove Widget title from the output'and
'Remove the default widget wrapper'options
Once you specify a Widget title, it will be also used in your list of widgets (instead of default ‘Custom Function #’).
There are already other WordPress plugins planned for a public release very soon. On this blog you can already see the Tabbed Widgets plugin which enables you to put any number of other widgets inside a single widget as tabs (with an option to rotate). Tabbed Widgets plugin has been released.
- I will try to get this plugin submitted at the official WordPress plugin repository, but so far nobody has reviewed my application.
- Please leave your ideas, suggestions and bug reports in the comments.