# How to check if searchform.php is being included as widget?

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?

-

## 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.

To inspect your sidebars, you can simply check the \$GLOBALS['wp_registered_sidebars'].
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.
I managed to do this with get_template_part(); - you just need searchform.php and searchform-different.php.