Search code examples
phpwordpresscodex

Deregister all styles and scripts from a certain Wordpress plugin


i want to wp_deregister all styles and scripts from a certain plugin.

For example

$plugin = getAllStyles('PLUGIN-NAME');      
foreach ($plugin as $value) {
    wp_deregister_style($value);
}

$plugin = getAllScripts('PLUGIN-NAME');      
foreach ($plugin as $value) {
    wp_deregister_script($value);
}

Is this possible?


Solution

  • Yes it's possible with MU-PLUGINs, that looks into request url and selectively disable plugins. That is possible because MU plugins are loaded before regular plugins.

    1) Create new Mu-plugin in /wp-content/mu-plugins/Removeplugin.php

    2) Add this Code

    /*
        Plugin Name: plugins remover
        Plugin URI: 
        Description:  Removes  plugin
        Author: Author name 
        Version: 1.0
        Author URI: 
    */
    

    3) Now add action hook option_active_plugins

    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
    function disable_plugin_callback_fun($plugins){
         $key = array_search( 'plugin_directory_name/plugin_filename.php' , $plugins );
         if ( false !== $key ) unset( $plugins[$key] );
    }
    

    Example if you remove the contact form 7 plugin css and js

    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
        function disable_plugin_callback_fun($plugins){
            $key = array_search(  'contact-form-7/wp-contact-form-7.php', $plugins );
        if ( false !== $key ) { unset( $plugins[$key] );}
    
    }
    

    NOTE: This action hook disable the plugin for site,So i suggest to Load plugin from selected page like bellow code Check the current page.

    $current_page = add_query_arg( array() );
    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
            function disable_plugin_callback_fun($plugins){
             $current_page = add_query_arg( array() );   
             if($current_page!='contact'){//contact us page url where to load plugin only 
             $key = array_search(  'contact-form-7/wp-contact-form-7.php', $plugins );
            if ( false !== $key ) { unset( $plugins[$key] );}
            }
        }
    

    For more information check this https://wordpress.stackexchange.com/questions/114345/load-plugin-selectively-for-pages-or-posts-etc

    EDIT

    If you need remove only all style and script from plugin. need to find all css and jquery and put it one by one in bellow function.

    No other way to remove all the style and script from plugin

    NOTE : It's only work when plugin used to add style or js useing wp_enqueue_style() and wp_enqueue_script( ) other wise this code is not working

    add_action('wp_print_scripts', 'disable_pluginsjs_selectively', 100 );
    function disable_pluginsjs_selectively( ) {
       wp_deregister_script( Name of the enqueued jQuery);
       wp_dequeue_style('Name of the enqueued stylesheet'); 
    }