Support » Plugin: Beautiful Cookie Consent Banner » Enqueue Scripts / Styles

  • Resolved David Goring

    (@dg12345)


    I believe you using the wrong hook to import your frontend assets.
    your using the wp_head hook to enqueue styles but it triggered too late and these styles and scripts are being deferred to the footer, which is normally fine for scripts but style link tags should always be in the <head> so they are loaded before page render.

    This has affected one of my sites as it’s cache plugin combines the css files, but because of this issue, the single combined file is now in the footer and so there is a period of time the user sees the site with no style at all.
    I have got round this by telling the cache plugin to ignore your stylesheet specifically but I shouldn’t have to do that.

    I suggest you make the following changes
    class-nsc_bar_frontend.php:22

    
        public function nsc_bar_execute_frontend_wp_actions()
        {
            add_action('wp_enqueue_scripts', array($this, 'nsc_bar_enqueue_scripts'));
            add_action('wp_footer', array($this, 'msc_bar_attachFooter'), 999); 
    
            add_shortcode('cc_revoke_settings_link_nsc_bar', array($this, 'nsc_bar_shortcode_revoke_settings_link'));
            add_shortcode('cc_show_cookie_banner_nsc_bar', array($this, 'nsc_bar_shortcode_show_cookie_banner'));
        }
    
        public function nsc_bar_enqueue_scripts()
        {
            wp_register_style('nice-cookie-consent', $this->plugin_url . 'public/v5/cookieNSCconsent.min.css', array(), '5');
            wp_enqueue_style('nice-cookie-consent');
            wp_register_script('nice-cookie-consent_js', $this->plugin_url . 'public/v5/cookieNSCconsent.min.js', array(), '5', true);
            wp_enqueue_script('nice-cookie-consent_js');
        }
    
        public function msc_bar_attachFooter()
        {
            echo '<script>window.addEventListener("load", function(){window.cookieconsent.initialise(' . $this->nsc_bar_json_with_js_function() . ')});</script>';
        }
    

    Alternatively if you need to keep the scripts out of this standard hook for the cookie blocking functionality then I suggest you move the stylesheet alone into the <head>

    class-nsc_bar_frontend.php:22

    
        public function nsc_bar_execute_frontend_wp_actions()
        {
            add_action('wp_head', array($this, 'nsc_bar_attachHeader'));
            add_shortcode('cc_revoke_settings_link_nsc_bar', array($this, 'nsc_bar_shortcode_revoke_settings_link'));
            add_shortcode('cc_show_cookie_banner_nsc_bar', array($this, 'nsc_bar_shortcode_show_cookie_banner'));
        }
    
        public function nsc_bar_enqueue_scripts()
        {
            wp_register_style('nice-cookie-consent', $this->plugin_url . 'public/v5/cookieNSCconsent.min.css', array(), '5');
            wp_enqueue_style('nice-cookie-consent');
        }
    
        public function nsc_bar_attachHeader()
        {
            wp_register_script('nice-cookie-consent_js', $this->plugin_url . 'public/v5/cookieNSCconsent.min.js');
            wp_enqueue_script('nice-cookie-consent_js');
            echo '<script>window.addEventListener("load", function(){window.cookieconsent.initialise(' . $this->nsc_bar_json_with_js_function() . ')});</script>';
        }
    
    • This topic was modified 2 years, 3 months ago by David Goring.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Nikel Schubert

    (@nikelschubert)

    Hi @dg12345,

    thanks a lot for pointing that out and giving advise how to solve that. Really appreciated. Will fix it as soon as possible.

    Do you mind sharing your cache plugins name and the config which is not working? So that I can easily test my changes?

    Thanks a lot!

    Regards
    Nikel

    Plugin Author Nikel Schubert

    (@nikelschubert)

    Hi @dg12345,

    just released your suggested changes. Thanks a lot!

    Regards
    Nikle

    Plugin Author Nikel Schubert

    (@nikelschubert)

    Hi @dg12345,

    sorry i didn’t ask: would you like to be mentioned as contributor for this plugin?

    Regards
    Nikel

    Thread Starter David Goring

    (@dg12345)

    No its fine, it was only a tiny change

    But thanks for offering.

    David

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Enqueue Scripts / Styles’ is closed to new replies.