Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
1  
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.