How to set a maximum word count on post titles

16 Feb 2011 By Code Snippets 1 Comment
16
Feb

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.

Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

One Response to How to set a maximum word count on post titles

  1. Thanks for the code, it will really help to set the title and make sure your title is too long. Kudos!!!

    Reply

Leave a Reply