I want to do the following in a plugin:
- hook to untrash_post
with 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_post
with 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 |2 Answers
Reset to default 2its 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.
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