Support » Plugins » Hacks » Make a specific search keyword redirect to a URL

  • Resolved ellewhin

    (@ellewhin)


    Hello,

    I would like my search form to return results in a normal manner except when one specific search term is entered I would like to direct the user to a specific url.

    Basically, when a user searches for the title of the blog I would like for them to be sent to the front page of the blog.

    How can I go about doing this?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • in the very top of your themes header.php you can add:

    <?php
    if(is_search()) {
     	if(preg_match('/\byoursitename\b/i', get_search_query()) == TRUE){
    		wp_redirect(home_url());
    		exit;
    	}
    }
    ?>

    change ‘yoursitename’ to your site’s well, name.

    http://www.php.net/manual/en/function.preg-match.php

    Hi tugbucket,

    Thanks for your reply!

    When I insert the code you provided and try a search for my site’s name I get the following error:

    Warning: Cannot modify header information – headers already sent by (output started at /blahurlblahblah/header.php:2) in /blahblahblahurl/wp-includes/pluggable.php on line 897

    Any ideas?

    Where did you put the code? It needs to be the very first thing in your theme’s header.php.

    Got it! Thanks a lot!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Make a specific search keyword redirect to a URL’ is closed to new replies.