How to cancel an action hooked to untrash_post? or any hook

I want to do the following in a plugin: - hook to untrash_postwith a custom function - do some checking inside custom function - cancel actual post restoring (untrashing)

I've tried with remove_action but doesn't seem to work. Can you point me into the right direction?

Code sample:

add_action( 'untrash_post', array( __CLASS__, 'static_untrash_handler' ) );

.....

public static function static_untrash_handler( $post ) {

// check stuff

// prevent post from being restored. How ?

}

Should I return something to 'break the cycle' ?

Thank you in advance!

Topic trash actions hooks customization Wordpress

Category Web


untrash_post fires before the untrashing happens. If you want to undo the untrashing, you should use the untrashed_post action which fires after it.


its a little clumsy but you could combine what you are already doing with the 'untrashed_post' hook which fires after the post has been untrashed

here are the rough steps:

  • use 'untrash_post' hook to do your checking (like you are now)
  • if you need to cancel the untrash, store a flag in post meta with ie. update_post_meta( $post_id, 'keep_trashed', true );
  • use 'untrashed_post' to check if the post should be retrashed with get_post_meta( $post_id, 'keep_trashed', true );
  • if so, use wp_trash_post( $post_id ); to retrash the post
  • dump the post meta delete_post_meta( $post_id, 'keep_trashed' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.