As I’ve mentioned previously I’ve been developing my own Wordpress theme. During the course of this I found, as many others have, the ‘Search’ widget available in the Wordpress sidebar does not use the searchform.php
file which template developers can provide. This irritated me and I was pretty sure the behavior of the ‘Search’ widget could be overridden in functions.php
. I was right…
I found the beginning of the answer in the Wordpress Widget API but to me the language of
It is possible for theme authors to define replacement widgets within functions.php. Replace an existing widget by registering its name with a new callback. An empty callback will unregister a widget.
was a little convoluted. As it turns out you need to do two things.
First, define a new function to use as the ‘widget callback’ or the function that is called when the widget is included in the page.
function bjs_widget_search($args) { extract($args); echo $before_widget; echo $before_title; _e("Search"); echo $after_title; include (TEMPLATEPATH . '/searchform.php'); echo $after_widget; }
Second, re-register the search widget using your new function name as the callback function.
$widgetOptions = array('classname' => 'widget_search', 'description' => __( "A search form for your blog")); wp_register_sidebar_widget('search',__('Search'),'bjs_widget_search',$widgetOptions);
And that’s all there is to it. You may notice that I actually did more than just make the search widget include searchform.php
I also added a header tag with a title for the widget. Keep in mind if you use ids for your search form elements this may invalidate your markup if the search form appears (or can) more than once on your page