skip to Main Content

I am working on a way to disable a specific plugin on a certain product page. I’ve cobbled this together from things I found online and the plugins code itself but its not working. Curious to have some fresh eyes have a look and let me know what might be failing. The post id of the product is 2679320. The actions I have set to remove are the ones referenced in the plugin wp_enqueue_scripts. Here is the code I’m trying by loading to snippets:

function remove__construct() {

global $post;
$ids = array(2679320);
if(in_array($post->ID,$ids)):
remove_action(‘wp_enqueue_scripts’,array($this,’enqueue_scripts’));
remove_action(‘plugins_loaded’,array($this,’load_txt_domain’),99);
remove_action(‘wp_footer’,array($this,’get_popup_markup’));
remove_filter( ‘pre_option_woocommerce_cart_redirect_after_add’, array($this,’prevent_cart_redirect’),10,1);

endif;
}

add_action(‘wp_head’, ‘remove__construct’, 1);

Any ideas why this isn’t working? What did I miss? Anyone have better way to do this?

2

Answers


  1. There are 2 ways to disable plugin.
    The first way is to create a custom plugin that removes the action that used to initialize your target plugin. The second way is to remove actions and filters which add scripts, styles and makes changes on a page.

    Either way you choose, you have to remove actions after they’ve been added and before they actually worked. That means that for the first way in most cases you have to use plugins_loaded hook which can’t be used in your functions.php (the first hook which can be used in functions.php is load_textdomain hook). in case you want to disable the plugin on certain pages you have to somehow get the current post ID, which isn’t so easy because global $post variable is not available yet (The earliest hook with $post is wp).

    Parameters for your remove_action depend on plugin add_action. The main point here is that all parameters of your remove_action must be the same as add_action parameters. Here are some examples :

    add_action('plugins_loaded', 'init_function_name'); 
    remove_action('plugins_loaded', 'init_function_name'); 
    
    add_action('plugins_loaded', 'init_function_name', 100); 
    remove_action('plugins_loaded', 'init_function_name', 100); 
    
    class Plugin_Classname {
      public static function init() {
        add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) );
      }
    }
    remove_action( 'plugins_loaded', array( 'Plugin_Classname', 'on_init' ) );
    
    class Plugin_Classname {
      public function __construct(){         
        add_action('plugins_loaded', array($this, 'init'), 99);
      }
      public static function get_instance(){
        if(self::$instance === null){
          self::$instance = new self();
        }   
        return self::$instance; 
      }
    }
    remove_action('plugins_loaded', array( Plugin_Classname::get_instance() , 'init'), 99);
    

    Let’s begin with the easiest way. Assume that removing scripts and styles is enough. Then you have to find wp_enqueue_scripts hooks in the plugin source. Eq.:

    class Xoo_CP_Public{
      protected static $instance = null;
      public function __construct(){
        add_action('plugins_loaded',array($this,'load_txt_domain'),99);
        add_action('wp_enqueue_scripts',array($this,'enqueue_scripts'));
        add_action('wp_footer',array($this,'get_popup_markup'));
        add_filter( 'pre_option_woocommerce_cart_redirect_after_add',  array($this,'prevent_cart_redirect'),10,1);
      }
      public static function get_instance(){
        if(self::$instance === null){
          self::$instance = new self();
        }   
        return self::$instance; 
      }
    }
    

    As we need global $post variable we gonna use wp hook. Place the code below in functions.php:

    function disable_plugin() { 
      global $post;
      $ids = array( 2679320 ); // Disable plugin at page with ID = 2679320
      if( in_array( $post->ID ,$ids ) ) {
        remove_action('wp_enqueue_scripts',array( Xoo_CP_Public::get_instance(),'enqueue_scripts'));
        remove_action('plugins_loaded',array(Xoo_CP_Public::get_instance(),'load_txt_domain'),99);
        remove_action('wp_footer',array(Xoo_CP_Public::get_instance(),'get_popup_markup'));
        remove_filter( 'pre_option_woocommerce_cart_redirect_after_add', array(Xoo_CP_Public::get_instance(),'prevent_cart_redirect'),10,1);
      }
    }
    add_action( 'wp', 'disable_plugin' );
    

    What if we want to remove an action is used to initialize this plugin? Let’s take a look at add_action:

    add_action('plugins_loaded','xoo_cp_rock_the_world');
    

    In this case we can’t use plugins_loaded hook because add_action is being called without priority parameter. If it’s being called with priority parameter we could just create disable-plugin.php file in /wp-content/plugins folder and place this code there:

    function disable_plugin() {
      remove_action('plugins_loaded', 'xoo_cp_rock_the_world', 100);
    }
    add_action('plugins_loaded','disable_plugin');
    

    But it’s useless in this case without priority parameter. Yet we can cheat! We don’t have to use any hooks and call remove_action directly. We should call it after target plugin add_action was called. Plugins are loaded in alphabetical order so if we named our plugin ‘zzz-disable-plugin.php` with this lines of code:

    /* Plugin Name: zzz-disable-plugin */
    remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
    

    The target plugin will be disabled. At all pages though. I haven’t find a way to get ID of current page on such an early hook. But we can use URI :

    /* Plugin Name: zzz-disable-plugin */
    if( 'product/polo' == trim( $_SERVER[ 'REQUEST_URI' ], '/' ) ) {
      remove_action('plugins_loaded', 'xoo_cp_rock_the_world');
    }
    
    Login or Signup to reply.
  2. You can use Plugin Organizer. It allows you to selectively disable a plugin on a page or a complete post type.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search