WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to put something like this in searchform.php:

<?php if($widget){ ?>
    //load serach form for sidebar...
<?php }else{ ?>
    //load different search form for page content (404 page)
<?php } ?>

How can I prepare $widget variable? Or maybe I should use another method? Like filter? Or get template part?

share|improve this question
up vote 1 down vote accepted

Check if searchform is included?

The get_search_form() function (that should be used) uses locate_template('searchform.php'); to display the search form.

The later returns (as you can see from its source)

the template filename if one is located

So if you're just wanting to catch the default way, then you should simply test

'searchform.php' === locate_template( 'searchform.php' ) AND print 'Search form displayed!';

You can maybe think of a hundred of other scenarios on how to include the search form, but you'll never catch them all.

The real problem (and I don't have a solution for that) is that this only checks the existence, not if it is included via a widget. So above is only meant to work from inside a (custom) widget.

Display widget programmatically

If you want to display a widget (in absence of others) via PHP code in your template, then go with the_widget() Source link.

Check your sidebars

To inspect your sidebars, you can simply check the $GLOBALS['wp_registered_sidebars'].

Check your widgets

Core also has a possibility to check the registered widgets: wp_get_sidebars_widgets(), which you can use to check for the existance of a widget.

share|improve this answer
I'm marking this as accepted answer because it contains a lot of useful information :) More than I expected to get and it helps also with other issues I'm encountering. Thanks! – Paul Nov 12 '12 at 13:00

I managed to do this with get_template_part(); - you just need searchform.php and searchform-different.php.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.