The Life and Times of Michael Shadle

Blog | Projects | Netflix

Why does WordPress allow empty post titles and bodies on publish?
Tuesday, July 3rd, 2007 at 2:53 pm

I have noticed (and also reported this on the WP site) that when a user clicks "Publish" it does not check for empty post titles or empty post bodies. I believe this is a flaw in design. The code is there to check, but it doesn't work right; it allows for empty post bodies or titles for drafts (which is okay, they might not have a title yet) but it should stop the user from being allowed to publish an empty post.

Below is the code I came up with after messing around for hours... it relies on the bundled jQuery in the latest WP 2.x versions. It does client-side checks to alert the user to fix the issue, but there is no graceful way (at least that I could find) to handle this if the user does not have JavaScript or anything enabled. If the server gets an empty title or body, it will empty all the fields out, essentially forcing the post to fail. The user *will* lose their work then (unless their back button works nicely.)

The typical user will typically have JS enabled, and they shouldn't be publishing without a title. However, I am dealing with some inexperienced bloggers - so I want to make sure I have all the bases covered. Once again, the reason this is so ugly is due to the fact that I do not modify the core WP code; everything I do is in plugin fashion (although I did suggest a quick fix in the WP core code in my forum post.) 

Links to my forum post and the bug submission:

Here's the code:

function empty_filter($text) {
        return '';
}

# if any field is empty, forcibly empty the fields so that it will fail post publishing
function check_empty_title() {
        if(isset($_POST['publish']) && $_POST['publish'] == "Publish") {
                if(empty($_POST['post_title']) || empty($_POST['content'])) {
                        add_filter('content_save_pre', 'empty_filter');
                        add_filter('excerpt_save_pre', 'empty_filter');
                        add_filter('title_save_pre', 'empty_filter');
                }
        }
}
add_action("load-post.php", 'check_empty_title', 1);

function check_empty_clientside() {
echo <<<EOF
<script language="javascript" type="text/javascript">
var wpJ = jQuery.noConflict();
wpJ(document).ready(function() {              
        wpJ("#post").submit(function(){
                if(wpJ("#title").val() == '') {
                                alert('You must put something in the post title!');
                                return false;
                } else {
                        if(tinyMCE.getContent() == '') {     
                                alert('You must put something in the post body!');
                                return false;
                        }
                }
        });
});
</script>
EOF;
}

add_action('admin_head', 'check_empty_clientside', 1);

This entry was posted and is filed under WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comment on this post:

Entries (RSS) and Comments (RSS). 15 queries. 0.194 seconds.