0

Is there a way to filter single.php so that if someone came from search.php it would display one thing, but if they came from anywhere else it would display another?

Thanks, Josh

3
  • Related wordpress.stackexchange.com/questions/155323/…
    – Tom J Nowell
    Jul 21, 2014 at 20:06
  • They are only related because they are discussing search.php...in one of them I am trying to figure out how to create a custom single.php file if someone comes from the search page, in the other I am trying to display the search term (what someone searches for) across selected pages in the site. Jul 21, 2014 at 20:23
  • If the answer to this question is no, then the other question is no, if the answer to this question is yes, then that answer is about 80%~ of the work needed to do the others. Any comments or answers are extremely relevant to eachother
    – Tom J Nowell
    Jul 21, 2014 at 20:48

2 Answers 2

1

You could hook into the the_permalink filter and check for is_search() to conditionally modify the URLs of your search results to include some additional parameters that your single.php then checks for. Something like this (may require tinkering):

add_filter( 'the_permalink', 'wpse155331_the_permalink' );
function wpse155331_the_permalink( $url ) {
  if ( is_search() ) {
    $url = add_query_arg( array( 'from_search' => 'true' ), $url );
  }
  return $url;
}
2
  • You should be able to incorporate the search term from your other related question in the same code - just add the search term as another key/value pair in the add_query_arg function. Jul 21, 2014 at 21:40
  • karpstrucking, I'm not sure how to integrate this, but I was able to find out how to do this another way. Thanks for the help :-) Jul 22, 2014 at 14:43
0

I created an if statement to solve my problem:

<?
    if (stripos(wp_get_referer(), "cat=")!==false) {
        echo "Came from Search";
    } else {
        echo "Didn't come from Search";
    }
?>

I am doing a category search, so I check to see if cat= is in the referrer url (or search url), if it is I execute some code, if not I execute some different code :-)

That's it!

Thanks,
Josh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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