3

I'll try explain this the best I can...

I have a search form on my homepage:

<form role="search" method="get" id="searchform" class="form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <label class="u-visuallyHidden" for="s">Search for property:</label>
    <input type="search" class="form-input search-bar-input" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="enter location here" autocomplete="off" />

    <button type="submit" class="btn search-bar-btn" id="searchsubmit">
        <span class="u-verticalAlignMiddle"><i class="icon-search"></i> <span class="u-visuallyHidden">Search</span></span>
    </button>
</form>

And then there's more in-depth search form in my 'search.php' template that holds a number of hidden fields that relate to some user input selectors. The search results then take the $_GET request and I build a custom $wp_query.

Everything works fine...but, I'd like to change my search URL to something like:

http://mydomain.dev/student/?s=

I've tried to change the action to esc_url( home_url( '/student/' ) ) but I just get a 404 when a search term is entered.

I've played around with the 'template_redirect' action and rewrite rules, but I don't want pretty permalinks because my refined search form adds a number of additional URL params. For example a users search could yield this URL:

http://mydomain.dev/?s=&letmc_type=professional&custom_locations=SomeCity&min_bedrooms=2&price_min=60&price_max=70

Also, is there a better way with WordPress of utilizing query args, rather than the $_GET superglobal?

  • @toscho I'm English :) less of the American English please....it's 'utilising' – Sam Holguin Jun 28 '15 at 13:27
  • Then refix it! :) – fuxia Jun 28 '15 at 13:32
1

The default "pretty" alternative to ?s= is /search/ - you should be able to use that right now.

If you want to change it to student, add this to your functions.php file:

function re_rewrite_rules() {
    global $wp_rewrite;
    $wp_rewrite->search_base        = 'student';
}
add_action('init', 're_rewrite_rules');
  • Thanks for the input, but I've literally just done this step. Using a pretty permalink breaks my current system when I add all my custom URL params on – Sam Holguin Jun 28 '15 at 13:42
  • 1
    I really just need mydomain.dev/student/?s= to work correctly from both the homepage and the search results page – Sam Holguin Jun 28 '15 at 13:43
  • What you're asking is to mix a "pretty permalink" with a query arg and I'm just not sure that it could be done in this case. Normally the rewrite should work, unless you have something else already loading at mydomain.dev/student/ (a page?) – Lucian Davidescu Jun 28 '15 at 13:57
  • There is a page....but I need to assign the 'search.php' template to it??? – Sam Holguin Jun 28 '15 at 14:02
  • That'd probably be useless, you'd lose the intended functionality of the page (unless it's something like an index or - to the same purpose - a search result for a space character). You should give a bit more context - why it doesn't already work as intended or what exactly you're trying to achieve as a whole (as there seems to be another issue along the way). – Lucian Davidescu Jun 28 '15 at 14:10
0

This works perfectly for me on my site: nnn.com.ng. Other solution throw a 404 error for me.

/**
* initiate a change of the wordpress default search page slug to the new search page slug.
*/
add_action( 'init', 'dsfs' );

function dsfs()
{
    $GLOBALS['wp_rewrite']->search_base = '%your_search_string%';
}

/**
* redirect default search page slug to the new search page slug.
*/

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

You will need to add "%" before and after "your_search_string" (e.g. %student%) for it to work. You may need to flush the cache if it is a production site with a cache plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.