Enqueue Scripts / Styles
-
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:22public 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>'; }
- The topic ‘Enqueue Scripts / Styles’ is closed to new replies.