1

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

  • 1
    Are you using query_posts somewhere on your search page. It is the most probable cause why is_search() would return false on the search page – Pieter Goosen Feb 18 '16 at 10:58
  • Hi @PieterGoosen thanks for your reply. That's an interesting catch. I've just tried deleting all contents of my search.php file but the die('it works'); still doesn't get executed. Any other ideas? – Joel Murphy Feb 18 '16 at 11:47
  • 3
    Something is breaking the main query on your search page. Do var_dump( $wp_query ); on your searh page and inspects the query object. It should tell you what is going on – Pieter Goosen Feb 18 '16 at 11:58
  • 1
    @JoelMurphy in a search, right under wp_head() in your header.php file grab print the WP_Query as Pieter suggested and put it here ( or in a pastebin ) - global $wp_query; print_r( $wp_query ); die(); This may bring some insight. – Howdy_McGee Feb 18 '16 at 21:50
  • 1
    Thanks for your reply. I just did this. Here is the output: pastebin.com/Z0DwDd20 – Joel Murphy Feb 18 '16 at 22:02
1

The var_dump from the main query is fine, is_search() returns true as it should. Also, the SQL query look fine for a search page.

This further deepens my suspicion that something is altering the main query right after it is successfully executed, and this happens most probably before wp_enqueue_scripts.

The most probable culprit here is query_posts. I can remember a while ago that someone had such an issue which turned out to be YOAST plugin (IIRC) which used query_posts or some bad filter or something, and the issue was solved by deactivating the plugin.

Your best place to start here would be to search your theme for query_posts and sort that out if there is any instances of query_posts. If it is not in a theme, you'll need to look into your plugins. Deactivate your plugins one by one and test after each one has being deactivated. You should be able to find the culprit with this method

EDIT

Try resetting the main query on wp_enqueue_scripts and then check if your styles and scripts are enqueued from your action. If so, that means that is_search() returns true, which 99.9% confirms the use of query_posts somewhere in your theme or plugin.

NOTE: This is just a test and not a solution.

add_action( 'wp_enqueue_scripts', function ()
{
    wp_reset_query();
}, 9 );

EDIT 2

I just realised that you have already deactivated all plugins. As you have stated, when you delete header.php, your results is ok.

Debugging

Unfortunately, the files you linked to does not help as it is basicly just all the themes in you themes folder, but here is some basic debug info

  • Code editors has quite good search functions which you can use to search for spefific strings inside all files within your theme. I would start by searching for all instances of query_posts, specially inside your functions files as it seems like the issue is caused by something hooked to an action hook which resides in the header or is actually hooked to wp_head

  • header.php houses the wp_head() function which is just a wrapper for the wp_head action. There are many action hooks (like wp_enqueue_scripts) hooked to wp_head. Check this post in order to get a list of everything hooked to wp_head. As a quick debugging check to check if the issue is caused by anything hooked to wp_head before going on a wild goose chase, simply delete wp_head() and check the is_search() condition. Obviously, if is_search() return true then, something hooked to wp_head is causing the issue.

  • header.php also houses functions like navigations menus, and most of these functions are filterable to filters. It might be that there is a bad filter that is used by one of these functions. If the above two bullet points where fruitless, delete these functions one by one to determine the bad apple. nce you have the bad apple, it is as easy as looking at the source code to determine which filters it uses and then looking for these custom filters in your theme's function files

0

As a partial answer to your question, I can explain your confusion over deleting the header file. The wp_enqueue_scripts hook is fired in the wp_head(), which must be in your header.php file. Your dummy header file doesn't include this, so the entire DA_script_enqueue function isn't running at all. When you delete the header.php file, WordPress uses a default instead, which does include wp_head.

For your original problem you'll need to go back to square one and debug things a bit at a time, but without losing the wp_head() function.

0

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.

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.