WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Make a specific search keyword redirect to a URL (5 posts)

  1. ellewhin
    Member
    Posted 3 years ago #

    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

  2. tugbucket
    Member
    Posted 3 years ago #

    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

  3. ellewhin
    Member
    Posted 3 years ago #

    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?

  4. tugbucket
    Member
    Posted 3 years ago #

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

  5. ellewhin
    Member
    Posted 3 years ago #

    Got it! Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic