0

I have mistakenly added a hook and now I want to remove it. I have already added a new action hook instead of that but for backward compatibility, I need to deprecate that hook.

Any Idea to achieve this?

0

Firstly, you will need to lave the hook there. Deprecation just signals to developers that they should not use it, and that it will be removed in future. So you're stuck with it for now.

So really deprecation is a communications issue, not really a code issue. One helpful thing WordPress does provide in this situation is the function do_action_deprecated(). This works exactly the same as do_action(), except that if a developer has WP_DEBUG enabled they will see a warning telling them that the hook has been deprecated. So the thing to do in your code would be to replace do_action() with do_action_deprecated() (or apply_filters() with apply_filters_deprecated() if it's a filter).

Other than that you should really just update your plugin's developer documentation to let developers know that the hook has been deprecated, and give them a reasonable time frame for removal. You should also make note of the deprecation in the change log for the release that includes the deprecation.

When, or if, the time comes to remove the hook, just remove the line entirely and inform developers of the removal in your documentation and change log.

That being said, if the only thing that's changed is the hook's name then you could just rename the hook and then hook into that hook yourself and fire the old hook:

function wpse_342000_old_hook( $arg1, $arg2 ) {
    do_action( 'old_hook_name', $arg1, $arg2 );
}
add_action( 'new_hook_name', 'wpse_342000_old_hook', 0, 2 );

But if you don't want to have to keep that code around, then you should still deprecate the original action:

function wpse_342000_old_hook( $arg1, $arg2 ) {
    do_action_deprecated( 'old_hook_name', [ $arg1, $arg2 ], '1.2.3', 'new_hook_name' );
}
add_action( 'new_hook_name', 'wpse_342000_old_hook', 0, 2 );

This way you can remove any reference to old_hook_name from your main code and move this code into a dedicated deprecated.php file that's out of the way. Just replace 1.2.3 wth the version number of your plugin that deprecated the hook.

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.