Add A Maximum Post Title Word Count

Snippets | By | 27 January 2011 | Comments 4

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

[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’);
[/php]

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.

[php]
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’);
[/php]

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.



4 Comments

  • January 27, 2011
    • January 27, 2011

Leave A Comment.