Search code examples
phpwordpresspagespeed

Is it a good idea to register style only in certain WP pages?


I have a page speed issue, the theme I bought is really crappy but i cannot change it now. I use WP_rocket, server have HTTP2 but still it is to many resources to load. So i try to reduce numbers of styles by wp_deregister_style and load it only when need. For example contact-form-7 front-end style I need only in .../contact page. It good idea ? Or it could be harmful?

function rs_deregister_css () {

global $wp;
 $url = home_url( $wp->request);
 $contakt = strpos($url,'contakt');

   if (!$contakt) {
        wp_deregister_style('contact-form-7');
  }

}

add_action( 'wp_print_styles', 'rs_deregister_css', 99);


Solution

  • Yes, its a very good idea since you only use the contact form in the contact page, don't forget to deregister the javascript file, too

    if (!$contakt) {
      wp_deregister_style('contact-form-7');
      wp_deregister_script('contact-form-7');
    }