0

In my functions.php I have:

// Load Google Optimize script in head
function us_homepage_optimize_test() {
    $site_id = get_current_blog_id();
    if(is_page(10) && is_main_site($site_id)) {
        wp_enqueue_script(
            'us_homepage_optimize_test',
            'https://www.googleoptimize.com/optimize.js?id=OPT-XXXXXXXX',
            array(),
            '1.0.0',
            false
        );
    }
}
add_action('wp_enqueue_scripts', 'us_homepage_optimize_test');

The Google Optimize script does not load and in debug.log I see:

PHP Notice: Function is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information.

How should I adjust my functions.php code, please?

Help appreciated.

2
  • 2
    Your code looked good to me, so try clearing your caches, comment out the add_action() line, then see if the same issue persists. If yes, then the issue is caused by another code like in a plugin. But if the issue didn't persist, uncomment out that line and see if the notice reappears.
    – Sally CJ
    Commented Aug 9, 2022 at 7:53
  • 1
    Thanks @SallyCJ, that fixed it for me, add an answer if you like.
    – Steve
    Commented Aug 10, 2022 at 2:18

1 Answer 1

2

Your code looked good to me, so I believed that the PHP notice was unlikely caused by the code in question. But that issue could probably be caused by a previous version of that code.

  • E.g. If you had used the init and not wp_enqueue_scripts hook to enqueue your script, then such notice is expected, because WordPress has not yet setup the main query by the the time init runs, (so whether the current request is for a Page, post, category, etc, is not yet known) and then causes WordPress to issue a notice saying <conditional tag> was called incorrectly.

Therefore, I guessed that:

  1. Either your server or WordPress used a cached version of the functions.php or debug.log file.

  2. Or that there's a plugin or another code in the active theme which caused the issue.

  3. Or maybe you were actually looking at an older entry in the debug log that was no longer valid for the current functions file? (Check the date — maybe it's August 7th and yet your functions file was edited on August 8th?)

And if it was not the 3rd one above, I would do the following to identify which one is causing the issue:

  • Truncate/empty or just delete your current debug log file, after backing it up if necessary.

  • Purge/clear all caches (server and also browser) — and optionally, temporarily disable caching.

  • Comment out the add-action line, i.e. do //add_action('wp_enqueue_scripts', 'us_homepage_optimize_test');.

  • Save the file and check if the same issue persisted.

If yes, then the code in question was not at fault. But if the issue persisted, then try uncommenting out the add-action line and see if the notice reappears.

So I hope that helps (others), at least in confirming whether that code was the culprit.

Your Answer

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

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