24

I'm using a plugin. It has an action like this.

add_action('publish_post', 'old_action');
function old_action($pid) {
    "code goes here"
    }
}

I'm writing a module for this plugin. So i need to override that old action function with my new action function.

This is my new function.

function new_action($pid) {
      "code goes here"
        }
    }

I want to replace that old_action function with my new_action function using hooks. Can anyone help me?

Thanks

1 Answer 1

44

You can use the remove_action() function, like this:

remove_action('publish_post', 'old_action');
add_action('publish_post', 'new_action');

It's important to note that if the old_action was added with a priority parameter, you must add that to the remove_action call, otherwise it will fail to remove it. There are other implications if the old_action was added within a class. See here for more info.

3
  • 3
    +1 with the clarification that the remove action must be called after the function it is removing is added. Jan 28, 2012 at 8:24
  • 5
    'after_setup_theme' seems like a good place for remove_action. See: wordpress.stackexchange.com/questions/170663/… Sep 10, 2016 at 19:48
  • I tried for quite some time and was scratching my head - in the end "after_setup_theme" made it work! Thanks!
    – frizzant
    Dec 27, 2023 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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