• Resolved makemewise

    (@makemewise)


    Hello, I am using twentysixteen theme and I would like to make it’s search bar to redirect to custom page, for example, if I type into the search bar “dogs” it would bring me to the page called “dogs” instead of search page. Or maybe it would be easier to get rid of magnifying glass button and make search bar so I could only type in it? Because I have a plugin that instantly suggests and takes me to page, if you press on suggestion. If so, how do I do that? Any help would be appreciated, thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • allm

    (@realblueorange)

    I am not sure how much you know so you can add to the following code, but I’ll give it anyway:

    function allm_redirect_search() {
    
    	if ( is_search() && isset($_GET['s']) ) { 
    
    		// add some code to decide where you want to go, based on $_GET['s']
    
    		// Go to other page:
    		wp_redirect( LINK-TO-PAGE );
    		exit();
    	}
    }
    add_action( 'template_redirect', 'allm_redirect_search' );

    Hope this gets you in the right direction.

    Moderator bcworkz

    (@bcworkz)

    The problem is if one of the suggestions is not picked, there would not be a page name matching the search term, so in that case you would want (I think) the user to go to the normal search results. It is only the auto-suggestions where you would want to go straight to the page, correct?

    It makes more sense then to identify the auto-suggest function that inserts the selection in the search box and instead have it redirect to the associated page. JS/jQuery redirects are more flexible than PHP because you are not forced to only redirect before content is output.

    JS/jQuery scripts can be overridden by ensuring your override is loaded after the script you are overriding. Using wp_enqueue_script() with a huge priority number will probably work, though identifying where and how the original script is loaded would be better.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Search redirect to custom page’ is closed to new replies.