How to set maximum post title length on WordPress

Sometimes you need to limit the maximum length of a WordPress post title, in such situation you can use below code snippet. Simply copy and paste below PHP code to your functions.php in theme folder and this will limit the length of title.

function limit_Title($title){
    global $post;
    $title = $post->post_title;
    if (str_word_count($title) >= 8 ) //Set the maximum number of words here
        wp_die( __('Error: Opps... your post title is over the maximum limit.') );
add_action('publish_post', 'limit_Title');