Viewing 15 replies - 1 through 15 (of 24 total)
  • Plugin Author SOGO

    (@orenhav)

    Hi, can you please share the site url?

    Thread Starter Grace Massa Langlois

    (@miavitadolce)

    Plugin Author SOGO

    (@orenhav)

    thanks, can you please check if there is wp_footer(); before the </body>

    Thread Starter Grace Massa Langlois

    (@miavitadolce)

    Yes there is.

    Plugin Author SOGO

    (@orenhav)

    it seems to be something else blocking it

    Thread Starter Grace Massa Langlois

    (@miavitadolce)

    Any idea what could be blocking it? Like I said works fine if I place it in the head section.

    Plugin Author SOGO

    (@orenhav)

    try to stop all your other plugins and test it

    Have same problem too… any idea how to fix this?

    Plugin Author SOGO

    (@orenhav)

    Do you have this in the page template you are using:
    wp_footer();

    Plugin Author SOGO

    (@orenhav)

    close as no reply

    Hi,

    I am experiencing the same problem –it works fine for wp_head();, not for wp_footer();– but in my case it happens just in some pages. The issue arose when I updated the theme that I am using (Modernize by GoodLayers), as those pages were working fine before.

    I have been doing some testing and could narrow it down. Now I know what is blocking it, which is the blog module (shortcode) of the theme, although I haven’t been able to dig into it deeper.

    PS: wp_footer(); is properly set in the template

    Plugin Author SOGO

    (@orenhav)

    Hi,

    the plugin is using the action add_action(‘wp_footer’,””)
    if you have “wp_footer()” in your page, the action will run.

    it means that either you don’t have this function in one of the pages or a plugin is blocking it

    will be happy to check if you send me connection details to your site

    I got the same error.
    After doing some debug i can see that the function hooked to wp_footer cannot get the global $post object and therefore not the post ID. This is not the case in the header thou, it works there. But it seems like WordPress clears the $post object before loading the footer.

    A possible alternative is to fetch the postID with another hook earlier in the loading process and store it in a custom global variable. Then retrieve it in the hooked footer-function.

    Another way is to reset the query and fetch the global var $wp_query.
    From this you can also extract the ID.

    Btw, nice plugin ?? Not many plugins that support custom scripts per page/post.
    Hoping this issue gets resolved ??

    Here is a change proposal for “oh-add-script-header-footer.php”.
    It may need some more checks, but i just did it without for now.
    This works for me ??

    add_action('wp', function(){
        global $post;
        $GLOBALS['oh_post_id'] = (isset($post->ID) ? $post->ID : false);
    });
    
    function oh_get_post_id()
    {
        global $oh_post_id, $post;
        if(isset($oh_post_id) AND is_numeric($oh_post_id))
        {
            return $oh_post_id;
        }
        else if(isset($post->ID) && is_numeric($post->ID))
        {
            return $post->ID;
        }
        return false;
    }
    
    require_once  plugin_dir_path( __FILE__ ) . "oh-settings-page.php";
    // add google analytics to footer
    function oh_add_script() {
        $oh_post_id = oh_get_post_id();
        if(!$oh_post_id) return false;
        $output = get_post_meta($oh_post_id,'_oh_add_script_header',true);
        echo stripslashes($output);
        if(oh_show_me_on('oh_posttype') &&  get_post_meta(oh_post_id,'_oh_add_script_header_hide',true) != 'on' ){
            $sogo_header_footer =  get_option('sogo_header_footer');
            if(isset($sogo_header_footer['oh_header'])){
                echo stripslashes($sogo_header_footer['oh_header']);
            }
        }
    
    }
    function oh_add_script_footer() {
        $oh_post_id = oh_get_post_id();
        if(!$oh_post_id) return false;
        $output = get_post_meta($oh_post_id,'_oh_add_script_footer',true);
        echo stripslashes($output);
        if(oh_show_me_on('oh_posttype_footer') && get_post_meta($oh_post_id,'_oh_add_script_footer_hide',true) != 'on' ) {
            $sogo_header_footer = get_option('sogo_header_footer');
            if (isset($sogo_header_footer['oh_footer'])) {
                echo stripslashes($sogo_header_footer['oh_footer']);
            }
        }
    
    }
    add_action('wp_head', 'oh_add_script');
    add_action('wp_footer', 'oh_add_script_footer');

    I have done some further research and it depends on how your theme is built. In some theme you can’t reach the global $post object in the footer. Therefore it’s a very good solution to catch the post ID with the hook “wp”, store it in a global variable and the catch it in the hook “wp_footer”.

    Hoping for a positive answer ??

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Adding script to footer not working’ is closed to new replies.