最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Is it possible to abort post update if specific conditions on metadata are met?

programmeradmin2浏览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.

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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(){})
发布评论

评论列表(0)

  1. 暂无评论