Edit:The problem were 2 folds - somehow the order list I found before indicated wp_head was before wp_enqueue_scripts but that is not the case. Found another more updated one and it is actually right after. see the order of hooks here. Changing to get_header hook remove the action in my own class successfully.

I was unable to remove the External Plugin action because the remove_action() expect argument with the instance of the class, which the External Plugin did not expose the instance in any way. I found a snippet that remove action/filter based on given class name and method instead of the instance of the class and method. Obviously the drawback is action from multiple instances of the same class will be removed but that's not my concern. snippet in here Hope this help someone else as well.

I am writing an add-on for an external plugin, such if a custom post meta exist for the page/post, disable/remove certain action added by the external plugin, as well as action added by my own plugin.

the action that I want to remove from the external plugin is called in a class construction.

class External_Plugin {
  function __construct() {
    add_action( 'wp_footer', array( $this, 'inject-code' ) );
new External_Plugin();

I hooked my check post meta code to wp_head as shown below

class My_Plugin {
  function __construct() {
    //check if dependency satisfied
    if (!class_exists('External_Plugin')){
      add_action( 'admin_notices', array( $this,'display_dependency_error_notice' ));
      add_action( 'wp_enqueue_scripts', array($this,'my_plugin_enqueue_scripts'));
      add_action( 'wp_head', array( $this, 'check_post_meta' ));

  function check_post_meta(){
    if (!is_admin()&&get_post_meta(get_queried_object_id(), 'fbcp_disable_chat',true)){
      remove_action( 'wp_footer', array('External_Plugin','inject-code',11 ));
      remove_action( 'wp_enqueue_scripts', array($this,'my_plugin_enqueue_scripts'));
new My_Plugin()

I can see from the log that my if block in check_post_meta() was indeed fired, but neither the wp_footer action by external plugin nor my wp_enqueue_scripts hooked action got removed. In my understanding, wp_head hook is already way after the class being constructed, hence my remove_action() are definitely called after the add_action() that I want to remove, but before the hooks these actions are fired. Can someone enlighten me what is wrong with my code, and how to use the remove_action()?

P.S. I am also confused when I can get the post ID in order to retrieve the post meta... why post id is only available by wp_head? as I tried the init hook and I could not get post ID at that stage.

Edit: Also found my answer to this question, should be able to use get_query_object_id() once after the parse_query hook, once again the order of WordPress hook in here.


According to the documentation for remove_action() the function name and priority must match the usage where the function was hooked. You have made a common sense assumption that a higher priority should be used for the removal but I believe that is the cause of the problem.

Docs: https://developer.wordpress.org/reference/functions/remove_action/

To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

Update your remove action statement to explicitly define a priority of 10:

remove_action( 'wp_footer', array('External_Plugin','inject-code', 10 ) );
  • I actually tried it all, no priority, 9,10,11. None of them works. I thought I would at least remove my own action as it is clearly executed after the add_action() and wp_head is definitely before wp_enqueue_scripts hook... but no that did not work. – Nick Li Apr 8 at 20:00
  • When is your check_post_meta() firing? It needs to run after the plugin registers the hook but before the wp_footer action is called. – jdm2112 Apr 8 at 20:11
  • Same as the code posted, added into wp_head – Nick Li Apr 8 at 22:33
  • Thanks for the help, I have found the issue and posted the solution in edit. – Nick Li Apr 9 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.