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
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 yourfunctions.php
(the first hook which can be used infunctions.php
isload_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 iswp
).Parameters for your
remove_action
depend on pluginadd_action
. The main point here is that all parameters of yourremove_action
must be the same asadd_action
parameters. Here are some examples :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.:As we need global $post variable we gonna use
wp
hook. Place the code below infunctions.php
:What if we want to remove an action is used to initialize this plugin? Let’s take a look at
add_action
:In this case we can’t use
plugins_loaded
hook becauseadd_action
is being called withoutpriority
parameter. If it’s being called withpriority
parameter we could just createdisable-plugin.php
file in/wp-content/plugins
folder and place this code there:But it’s useless in this case without
priority
parameter. Yet we can cheat! We don’t have to use any hooks and callremove_action
directly. We should call it after target pluginadd_action
was called. Plugins are loaded in alphabetical order so if we named our plugin ‘zzz-disable-plugin.php` with this lines of code: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 :
You can use Plugin Organizer. It allows you to selectively disable a plugin on a page or a complete post type.