0

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.

2

There'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(){})
4
  • Thank you Himad, I've seen that action too, but didn't know if there were anything to stop the process of updating post. You think the wp_safe_redirect is a good and standard way to achieve that? I'll test as soon to check and select your reply. It would be great if we could also notify the user about the post not being updated and for what reason. – Jazzpaths Jan 24 '20 at 13:12
  • As far as I've seen in the WordPress source code, it is the standard way to abort a process when certain conditions are not met. The die() function prevents the script to keep running and the user is redirected, creating a new request. – Himad Jan 24 '20 at 13:19
  • Regarding the notification, you can hook to the admin_notice action and print your message to the screen. However, since a redirect kills the process, any hooks you added will be lost. A solution I found to this problem is adding the message to the session, and then at the beggining of every request, hook to admin_notice and if the session has a 'flash_session_message' entry, for example, show it's content and then delete it to prevent it from showing again. – Himad Jan 24 '20 at 13:22
  • 1
    You rock man, yeah! Thanks a lot. – Jazzpaths Jan 24 '20 at 15:06

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.