• Resolved scott80109

    (@scott80109)


    The form action for the Bop search form goes to my root URL. How do I change that to go to a specific page on my site? I want the search form to be submitted to /advanced-search/.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author joe_bopper

    (@joe_bopper)

    Hi Scott,

    There isn’t an immediately simple way to do this in this plugin as it is a somewhat unusual use-case.

    Are you using another plugin to get the advanced search page? If so, how do you get the relevant search box in your site (e.g., is it a shortcode, a php function, …)?

    Cheers,
    Joe

    Thread Starter scott80109

    (@scott80109)

    I am just trying to figure out a way to override default search handling. The site that I’m building uses iFrames to CNET’s product sales catalog. So for example, I have a page called “Laptops”, which contains an iFrame to CNET’s product catalog showing laptops.

    What I want is a search box (preferably in the main menu) that searches the CNET catalog instead of searching the WordPress site itself. So I have the advanced-search page that contains an iFrame to CNET’s search tool. In the iFrame’s shortcode, I need to append the search term to the iFrame’s URL.

    Plugin Author joe_bopper

    (@joe_bopper)

    Ah, okay. In which case, this little guide’ll help you:

    https://wpengineer.com/2258/change-the-search-url-of-wordpress/

    It lets you know how to redirect when wordpress is usually going to it’s default search page. From the guide with a couple of changes for best practice:

    add_action( 'template_redirect', function() {
      if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        wp_die();
      }
    } );

    The trouble here is that it redirects all searches (thus in your situation preventing you from ever searching wordpress). Hence you may well wish to use:

    add_action( 'bop_nav_search_hidden_inputs', function( $item, $depth, $args ){
      ?>
      <input type="hidden" name="search-type" value="advanced-search">
      <?php
    }, 10, 3 );

    in order to identify this search form from others. And then you’d use:

    add_action( 'template_redirect', function() {
      if ( is_search() && ! empty( $_GET['s'] ) && isset( $_GET['search-type'] ) && $_GET['search-type'] == 'advanced-search' ) {
        $page_url = home_url( "/advanced-search/" );
        wp_redirect( $page_url . '?q=' . urlencode( get_query_var( 's' ) ) );
        wp_die();
      }
    } );

    which is adjusted to your use-case. Finally, in your advanced-search page template, add the following to the top:

    $search_query = isset( $_GET['q'] ) ? wp_strip_all_tags( $_GET['q'] ) : '';

    Then $search_query has the value to put in the iframe url.

    Note the importance of wp_strip_all_tags; it prevents XSS attacks.

    Cheers,
    Joe

    P.S. Just to note that these things are, by and large, outside the remit of this plugin (because it is largely about what is happening after a search is made rather than about where the search form is put in the page), which is why they are not included as options or anything like that.

    Plugin Author joe_bopper

    (@joe_bopper)

    Hi Scott,

    As it’s been a while, I’m assuming this resolved your issue and so am marking the thread as such. Please, feel free to switch it back and update if this is not the case.

    Cheers,
    Joe

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change form action’ is closed to new replies.