wp_enqueue_scripts not called on search page?

I’ve written a custom theme. It’s pretty complex so I won’t post a huge code block here for everyone’s sanity.

In my functions.php file I have the following hook being called:

function DA_script_enqueue() {
    if( is_404() ){ 
        //load scripts with wp_enqueue_script() 
    }
    if( is_search() ){ 
        //load scripts with wp_enqueue_script() 
    }
}
add_action( 'wp_enqueue_scripts', 'DA_script_enqueue');

What happens on my wordpress website when my theme is activated is that no scripts get added to the search.php page but they do get added to the 404.php page which is very odd! In fact, not even killing the page works with die();

if( is_search() ){ 
   die('it works');
}

This leads me to think that the condition is never being met within the hook override for wp_enqueue_scripts. I’m not sure if this is a bug, or if my logic is just incorrect and I’m calling the wrong functions. What else can I try to make my logic work?

Thanks

Edit:
Okay an update: I just tried the above code in another theme and it DOES do what it’s supposed to! I guess the question I should now be asking is what can I do to ensure is_search() returns true? So far I’ve disabled all plugins and removed a query_posts() call from index.php, but still facing issues 🙁

Edit #2:
After 9 hours of debugging this (sigh) I’ve finally found what’s causing the problem (but no solution to it – yet!)

I created a test wordpress site and copied over my custom theme files. I deleted them one by one until I had the bare minimum files needed for a theme and that’s when I found out what was causing the problem. It seems having a header.php file makes is_search() return false. Very strange!

enter image description here

enter image description here

enter image description here

As you can see from the screenshots above, as soon as I delete the header.php file everything is fine as is_search() returns true.

What on earth could be causing this very strange issue? I have other themes with a header.php file in them and they work fine…

I’m going to continue to play around with this but here are my source files just in case:

search.php:

<?php get_header(); ?>

search page

<?php get_footer(); ?>

functions.php:

<?php
        // This function will load certain scripts on certain pages - saving on bandwidth and server resourses.
    function init_scripts()
    {
        if ( is_search() ) {
            die('Yes this is the search page loading from the init_scripts function');
        }
    }
    add_action( 'wp_enqueue_scripts', 'init_scripts' );
?>

header.php:

header

Edit #3:
Here is the output of $wp_query also http://pastebin.com/Z0DwDd20

Edit #4:
Time for bed now so I guess I’ll have another shot at this tomorrow. Here’s the theme files just in case anyone is interested: https://drive.google.com/folderview?id=0B1zYR-LjR-j9NFV2ZXg3V0NJblk&usp=sharing

3 Answers
3

When using wp_enqueue_script, you probably passing true for in_footer parameter. If so, then check that your search.php is calling get_footer() function at the bottom.

Leave a Comment