Add A Maximum Post Title Word Count

Snippets | By | 27 January 2011 | Comments 2

For whatever reason occasionally in our WordPress installs we need to limit certain things, this could be for aesthetic purposes or SEO, so today I will show you how to limit the post title word count.

This snippet will be added to your functions.php file, so backup before you begin.

functions.php

function titleCount($title){
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 10 )
wp_die( __('Error: this title is over the word limit') );
}
add_action('publish_post', 'titleCount');

This snippet will now return an error if the word count is over 10 words, you can of course alter the error message and the word count, for example if you wanted 20 words as a limit and a alternate error message.

function titleCount($title){
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 20 )
wp_die( __('Error: title over 20 words') );
}
add_action('publish_post', 'titleCount');

This simple snippet can be handy if your design is constrained in any way, but whilst this might be an easy fix it might be beneficial to alter your design to rectify the constraint.



Paul Maloney is a UK based web designer/developer and runs the design agency Tropica. He particularly enjoys using and working with WordPress and has a keen interest in typography. You can also follow Paul on Twitter.

Visit Paul Maloney's website

2 Comments

  • January 27, 2011
    • January 27, 2011

Leave A Comment.