I have a specific CSS-file for my search results, so the search.php template. I want a PHP if-condition to determine if the current template is the search.php template, so that I can load that CSS-file with wp_enqueue. The condition should work in the functions.php file.

I'm not entirely sure if I can use is_search() for this. Can anyone confirm I can use is_search() and, if that's not the case, provide an alternative?

Many thanks in advance!


WordPress conditional functions will not tell you which template file is used. They tell you what the current request is for. Which file is loaded for that type of request is determined by the template hierarchy.

So, is_search() will tell you if search.php is being used if the current request is a search, and the current theme includes a search.php. If the theme does not include search.php, is_search() will still return true if index.php is used, as per the template hierarchy.

That's the long way of saying that if your theme has search.php, then yes, use is_search().

| improve this answer | |
  • Thanks Jacob! Also for the explanation about the type of request and the template hierarchy. (PS: I can accept it in 6 minutes.) – ralphsmit Jul 6 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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