I’m using the twentysixteen theme default. I have some information on this site but when someone uses the search box that I have in the sidebar (widget) I’d like it to search an external WordPress website of mine that has more relevant information.

So on domain1.com when someone clicks on the widget and puts in their search term then clicks on the search button or presses enter i’d like it to search on domain2.com

Scenerio:

  1. Visitor clicks on search box that’s on domain1.com
  2. Types in bobcat equipment as their search term
  3. Presses enter
  4. Search Directs to: http://domain2.com/?s=bobcat+equipment

My searchform.php file looks like this:

<?php
/**
 * Template for displaying search forms in Twenty Sixteen
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */
?>

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">
    <label>
        <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentysixteen' ); ?></span>
        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'twentysixteen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
    </label>
    <button type="submit" class="search-submit"><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentysixteen' ); ?></span></button>
</form>

Any help with the modification would be appreciated.
Narrowing this down to this line: <form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( "https://wordpress.stackexchange.com/" ) ); ?>">

Help with using a custom URL here instead of echoing the home url.

1 Answer
1

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *