Modifying widget search box

The default Search widget uses the get_search_form() core function to display the search form.

You can therefore edit the search form’s action

<form role="search" method="get" class="search-form" action="http://domain2.com">

in the child theme’s searchform.php file.

Here we assume domain2.com also supports the default WordPress search parameters.

If we don’t want to modify the file, we can adjust the output of home_url() via filters:

add_filter( 'widget_display_callback', function( $instance, $obj, $args )
{
    // Only target Search widgets
    if( 'search' === $obj->id_base )
        add_filter( 'home_url', 'wpse_search_domain', 10, 2 );

    return $instance;
}, 10, 3 );


function wpse_search_domain( $title, $post_id )
{
    // Only run once
    remove_filter( current_filter(), __FUNCTION__ );

    // Replace the home url with domain2.com
    return 'http://domain2.com/';
}

where target the first home_url() call in each Search widget.

Note that the first approach will modify the output of all get_search_form() calls, but the second approach will only modify the output of get_search_form() wihtin the each Search widget.

Leave a Comment