Wordpress-取消后期保存过程

tit*_*oyo 5 wordpress

当帖子内容包含特定字符串时,我需要中止帖子保存过程,然后向用户显示一条消息。

我找到了一种显示消息的方法,但没有找到拒绝后期保存的方法。

到目前为止,这是我所做的

 add_action( "pre_post_update", "checkPost");
 function checkPost($post_ID) {
      $post = get_post($post_ID);

      $postContent = $post->post_content;

      if ( wp_is_post_revision( $post_ID  ) )
           return;

      if(preg_match("/bad string/", $postContent) == 1) {

           //
           // cancel post save
           //

           // then
           add_filter("redirect_post_location", "my_redirect_post_location_filter", 99);
      }
 }

 function my_redirect_post_location_filter($location) {
      remove_filter('redirect_post_location', __FUNCTION__, 99);
      $location = add_query_arg('message', 99, $location);
      return $location;
 }

 add_filter('post_updated_messages', 'my_post_updated_messages_filter');
 function my_post_updated_messages_filter($messages) {
      $messages['post'][99] = 'Publish not allowed';
      return $messages;
 }
Run Code Online (Sandbox Code Playgroud)

Tri*_*hul 1

希望这可以帮助您检查帖子内容

function to_err_is_human( $post_id ) {
    // If this is just a revision, don't check
    if ( wp_is_post_revision( $post_id ) )
        return;
    $post_content = wp_unslash(!empty($_REQUEST['content']) ? $_REQUEST['content'] : $post_data['content']);
        if ($post_content=="abc")
        { add_filter("redirect_post_location", "my_redirect_post_location_filter", 99);}
}
add_action( 'save_post', 'to_err_is_human' );
function my_redirect_post_location_filter($location) {
      remove_filter('redirect_post_location', __FUNCTION__, 99);
      $location = add_query_arg('message', 99, $location);
      return $location;
 }

 add_filter('post_updated_messages', 'my_post_updated_messages_filter');
 function my_post_updated_messages_filter($messages) {
      $messages['post'][99] = 'Publish not allowed';
      return $messages;
 }
Run Code Online (Sandbox Code Playgroud)