I have a custom post type which has some metadata to check in order to be saved or updated. Now I would like to know if it's possible to abort the post creation or update if the check on metadata fails.
To give a simple context and fix the idea, let's say my custom post has a metabox with two date input, let's call them date_start and date_end. When the user try to publish or update the post in the backend I would like to check those metadata and if date_start >= date_end the process aborts without actually update or insert the post into the database. The logic I'm trying to achieve would be something like this:
add_action( 'before_updating_post', check_metadata );
function check_metadata() {
if ( condition ) {
continue post save/update
} else {
abort post save/update // The post will not be saved/updated
}
}
Where 'before_updating_post' is a fantasy name for an hook which allows me to abort the process of updating the post.
I've looked at some questions related to post update hooks but was unable to find some which allows to abort the process being most of them related to data manipulation before saving.
I have a custom post type which has some metadata to check in order to be saved or updated. Now I would like to know if it's possible to abort the post creation or update if the check on metadata fails.
To give a simple context and fix the idea, let's say my custom post has a metabox with two date input, let's call them date_start and date_end. When the user try to publish or update the post in the backend I would like to check those metadata and if date_start >= date_end the process aborts without actually update or insert the post into the database. The logic I'm trying to achieve would be something like this:
add_action( 'before_updating_post', check_metadata );
function check_metadata() {
if ( condition ) {
continue post save/update
} else {
abort post save/update // The post will not be saved/updated
}
}
Where 'before_updating_post' is a fantasy name for an hook which allows me to abort the process of updating the post.
I've looked at some questions related to post update hooks but was unable to find some which allows to abort the process being most of them related to data manipulation before saving.
Share Improve this question asked Jan 24, 2020 at 8:17 JazzpathsJazzpaths 1033 bronze badges1 Answer
Reset to default 2There's an action hook, pre_post_update
that gets executed just before updating a post.
add_action('pre_post_update', function($post_id, $data)){
if(!isset($_POST['date_start']){
wp_safe_redirect("post.php?post={$post_id}");
die();
}
}
I didn't find any action that gets executed right before creating a post, however, I did find a filter that does and will work to validate data / abort with a redirect just like the function above.
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
add_filter('wp_insert_post_data', $data, $postarr, function(){})