How to set a maximum word count on post titles

How to set a maximum word count on post titles

This WordPress code snippet demonstrates how to set a maximum word count on post titles.

How to set a maximum word count on post titles

How to set a maximum word count on post titles

There may be times when you want to limit the amount of characters that are displayed when displaying the title of your posts. Using this simple code snippet you can determine exactly the amount of characters that are displayed when displaying the characters. This may be used when listing all your latest posts on your home page for example.

 Set the maximum word count

You can adjust the amount of characters returned by changes the number in the snippet.
To apply this snippet, simply paste the following code in your WordPress theme functions.php file located in your current theme:

1
2
3
4
5
6
7
function maxWord($title){
    global $post;
    $title = $post->post_title;
    if(str_word_count($title) >= 10 ) //set this to the maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWord');
function maxWord($title){
    global $post;
    $title = $post->post_title;
    if(str_word_count($title) >= 10 ) //set this to the maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWord');

Be sure to check out our other great WordPress Code Snippets and WordPress Articles.

One thought on “How to set a maximum word count on post titles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">