A
A
Alex2018-06-21 14:24:30
WordPress
Alex, 2018-06-21 14:24:30

How to stop creating/updating a post in Wordpress?

I added an arbitrary text field to the records. What hooks/filters should be used to clear this field, and if it is empty - cancel the creation/update of the record: throw an error, or something else - the main thing is that the new record does not appear or is not updated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrTimon, 2018-06-21
@Kozack

I used the filter wp_insert_post_empty_content if the result returns true the post is not created.

add_filter( 'wp_insert_post_empty_content', 'check_list_post', 99, 2 );
function check_list_post($maybe_empty, $postarr)
  {
    global $post_type;
    if ( $post_type != 'MY_POST_TYPE' ) return $maybe_empty;

    if ( true === wp_is_post_revision( $postarr[ 'ID' ] ) || $postarr['post_status'] == 'auto-draft' ) {
      return $maybe_empty;
    }	

    if (  isset($_REQUEST['action']) && $_REQUEST['action'] == 'trash' ) {
      return $maybe_empty;
    }

    
    if ($something_wrong) {
//		getFlashMessage()->setMessage('Error: Insert error','error'); // add error to notice
                    return true;
                }

    return $maybe_empty;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question