• squasher

    (@squasher)


    Hi,

    I noticed that it is possible to save empty posts (no title, content and excerpt). I think this is very strange. I tested it on various sites to see if I’m right and I believe I am.

    How can I prevent this? I don’t want this behaviour, especially with custom post types.

    In post.php I found the following part:

    if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
    		if ( $wp_error ) {
    			return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
    		} else {
    			return 0;
    		}
    	}

    I would expect this to throw an error but I guess I’m wrong.

    So next I tried this filter, but that didn’t work:

    //We do not want to be able to save posts without title and content:
    add_filter ('wp_insert_post_empty_content', function() { return false; });

    So how can I prevent saving empty (custom) posts? Any suggestions?

    Thanks!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    That empty content filter defaults to false, you should return true for that, at least that appears to be the intention. On my site returning true causes all sorts of warnings and strange behavior. It does prevent empty posts though.

    Another thing you could do is hook ‘wp_insert_post_data’. If the passed data fields are empty, simply die with an error message. Check for specific details like post type to avoid dying needlessly.

Viewing 1 replies (of 1 total)
  • The topic ‘Prevent saving empty posts’ is closed to new replies.