Adding Extra Default Available Wordpress Widgets

Today I was working on my band’s website, which is based on Wordpress. The site contains two different sidebars, 1 and 2. I encountered a problem when after I added the Search sidebar widget to sidebar 1, it was ‘used up’ and I could not add it to sidebar 2 also. After some brief searching and thinking, I realised what I could do.

In the file /wp-includes/widgets.php there is a function wp_widgets_init() and it is in this function that the sidebars are registered. To duplicate the Search sidebar widget you would duplicate this code:


$widget_ops = array(’classname’ => ‘widget_search’, ‘description’ => __( “A search form for your blog”) );
wp_register_sidebar_widget(’search’, __(’Search’), ‘wp_widget_search’, $widget_ops);

However, rename the first argument of wp_register_sidebar_widget() to something like ’seach2′, so that it does not conflict with the original Search sidebar.

Now, instead of adding the duplication directly in the wp_register_sidebar_widget() function in the file, I opted to create a simple plugin to do the job. At the end of the function, you will notice the plugin hook do_action(’widgets_init’). It is this hook which you can use in the plugin to add the extra sidebar registrations. Here is the code for a simple sample plugin I created which adds an extra Search widget and an extra Links widget.

/*
Plugin Name: Extra Widgets
Plugin URI:
Description: This plugin adds an extra available search widget to your Wordpress installation's sidebar system.
Author: Simon D'Alfonso
Version: 1.0
Author URI: http://sjdalf.com
*/

function add_search() {
$widget_ops = array('classname' => ‘widget_search’, ‘description’ => __( “A search form for your blog”) );
wp_register_sidebar_widget(’search2′, __(’Search’), ‘wp_widget_search’, $widget_ops);
}

function add_search() {
$widget_ops = array(’classname’ => ‘widget_links’, ‘description’ => __( “Your blogroll”) );
wp_register_sidebar_widget(’links2′, __(’Links’), ‘wp_widget_links’, $widget_ops);
}

add_action(’widgets_init’, ‘add_search’);
add_action(’widgets_init’, ‘add_link’);
?>

Tags: , ,

Related Posts

Leave a Reply