admin管理员组

文章数量:1434921

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!

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!

Share Improve this question asked Feb 26, 2016 at 16:35 SXNSXN 111 bronze badge 2
  • Can you just update the post status to trashed in that hook? What does it report as the status in the handler? – jgraup Commented Feb 26, 2016 at 16:44
  • You can't set it to trashed in that hook, because it'll just get immediately set back, but maybe you can hook untrashed_post instead, which runs immediately after it's untrashed instead of before. See it in source here. – Milo Commented Feb 26, 2016 at 16:48
Add a comment  | 

2 Answers 2

Reset to default 2

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' );

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.

本文标签: How to cancel an action hooked to untrashpost or any hook