• Resolved SRD75

    (@srd75)


    I need to load a Google Optimize script in the head of the home page of the main site in a MU network setup. We have code:

    // 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    Use like this,

    <?php 
    global $post;
    
    if( $post->ID == 123) { ?>
    
         /*your code goes here*/ 
    
    <?php } ?>
    • This reply was modified 1 year, 11 months ago by Jan Dembowski.

    @srd75 FWIW, I added your code to my testing site, and I don’t get the same warning. What types of pages are the warnings coming from? Generally, that type of error means you’re trying to run a function in a hook that is firing too early, but from what I can tell, is_page() should be able to run within wp_enqueue_scripts.

    If you notice the warning comes from a specific type of page or post, you can add a check to ensure you’re on a type of page that should be able to use the function.

    Good luck, and I hope that helps!

    Thread Starter SRD75

    (@srd75)

    We ended up using Segment to load the Google Optimize code.

    Perhaps there was conflicting code.

    Thanks both of you for your help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP Notice: Function is_page was called incorrectly. Conditional query tags do n’ is closed to new replies.