How to hide plugins style sheets in wordpress

i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.

we have used following code in plugin, but it doesn’t work. please help how to use it.

if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');

Related posts

2 comments

  1. If you are modifying your own plugin I see no reason your code wouldn’t work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don’t intend.

    However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:

    if (is_singular('post')) {
        wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
    }
    

    However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update… it is very difficult to maintain that sort of modifications in the long run.

    Instead make a new plugin and modify whatever you need from there.

    What you want to achieve can be accomplished by:

    Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.

    Inside that folder create a new file called my_load_reducer.php

    Paste this in the file:

    <?php
    /*
    Plugin Name: My Load Reducer
    Description: Removes unneeded and unwanted stylesheets from other plugins
    Version: 0.1
    */
    
    //Use a class to avoid conflicts
    class my_load_reducer {
        function __construct() {
            //Hook into wp_enqueue_scripts with a high priority
            add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
        }
        function deregister_styles() {
            //Check that current post is not a single post
            if (!is_singular('post')) {
                //deregister the stylesheet - this removes the twentyfifteen
                //main stylesheet - obviously you need to substitute the handle
                //of the stylesheet you actually want to remove
                wp_deregister_style( 'twentyfifteen-style' );
            }
        }
    }
    
    //Instantiate the class
    $my_load_reducer = new my_load_reducer();
    

    Activate the plugin through the wordpress admin.

  2. You can remove perticular plugin css on selected page.

    below code is remove plugin css to other pages and display only on post pages:

    /*disable loading plugin css to page and load on post page*/
    
    add_action('wp_print_styles', 'my_deregister_styles', 99999);
    
    function my_deregister_styles() 
    {
      if(!is_single())
      {
          wp_dequeue_style('plugin-css-handle');
          wp_deregister_style('plugin-css-handle');  
      }
    }
    

    where ‘plugin-css-handle’ is perticular plugin’s css handle which you want to remove.

Comments are closed.