• Hi,

    Not sure if anyone can help or not but here’s what I want to do…

    When a user searches for something using my wordpress search box and doesn’t get a match then I want to redirect that user to an URL but which URL they get redirected to depends on what they were searching for.

    So for example I have redirect URL A.
    Redirect URL A takes a user to a website about cats.
    When a user searches for anything with the word cat or any other pre-defined search term and no results are found then redirect that user to URL A.

    Ideally I’d like to do it something like this:

    Redirect URL A:
    Keyword or Phrase 1 (required)
    Keyword 2 or Phrase (optional)
    ” repeat “

    Redirect URL B
    Keyword 1 or Phrase (required)
    Keyword 2 or Phrase (optional)
    ” repeat “

    Any ideas?

    Please try to be specific and simplistic in your terminology too as I’m not too familiar with coding or wordpress core. I can do simple programming things but nothing mega advance.

    Thanks in advance!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    An automatic redirect may not be the best approach. In PHP you cannot redirect if any output at all has been sent. Thus you would need to redirect before any template is loaded, meaning you cannot inform the user what’s going on. From their perspective, they search your site for a particular term and the next thing they know they are on some strange website. Many users will assume your site has been hacked.

    I would suggest you simply place topical links on the usual “Nothing matched your search terms” page. Which template does this is theme specific, but try starting with search.php. (Note: When you find yourself editing theme templates, you should create a child theme to contain your custom versions.)

    You can extract the search terms used from $_SERVER['QUERY_STRING']. You can use a switch/case structure to output links based on which search terms were used. Since you are not redirecting, you can output multiple links and avoid complex logic with required and optional terms.

    If you are really set on a redirect, you can use javascript to redirect from the nothing matched page.

Viewing 1 replies (of 1 total)
  • The topic ‘Redirect unsucessful searches to specific URLs’ is closed to new replies.