Home > WordPress > Why does WordPress allow empty post titles and bodies on publish?

Why does WordPress allow empty post titles and bodies on publish?

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() {
      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;

add_action('admin_head', 'check_empty_clientside', 1);
Categories: WordPress
  1. Freakenstein
    September 13th, 2009 at 02:59 | #1

    This is a nice idea, how ever, I can't get this to work.
    Added this in a PHP file, with wrapped around it...
    but somehow it gives a 'Parse error: syntax error, unexpected $end'

  2. mike
    September 13th, 2009 at 03:25 | #2

    I just put this into a file by itself and ran a lint check and it passed without an issue. Something must be awry with how you've pasted it or something else in the file...?

  3. Freakenstein
    September 13th, 2009 at 03:57 | #3

    Hmm, nothing :S

    btw: why I'm interested in this idea,
    i was thinking of an option to set a minimum postcount before allowing an post to be published,
    so if you can check if a post is emtpy or not, it's also possible to set a check for the number of words.

    why is this interesting? well, when having a wordpress with multiple authors, like a sportsclub, you can set a minimum wordcount so everyone writes enough words to get a decent introduction/excerpt. (instead of just posting a photogallery)

    anyways, i'm not that experienced, since I can get this script to work allready :S

  4. mike
    September 13th, 2009 at 09:58 | #4

    There's probably already modules for that.. I'd use jQuery to do the word count and reject it on the frontend for a better user experience and enforce it on the backend. Sort of like I did with this. Pretty simple stuff 🙂

  1. No trackbacks yet.
You must be logged in to post a comment.