How to limit characters of the post title?

How do I limit the number of characters in a post title? I’ve seen it done in Yoast’s WordPress SEO plugin but I want to do something similar for the WP title.

I checked out a plugin called Limit a post title to X characters but it seems like it’s been abandoned and it also breaks the Edit Media page since the media too has a title.

Related posts

3 comments

  1. You could do this :

    add_action('publish_post', 'wpse_107434_title_max_char');
    function wpse_107434_title_max_char() {
      global $post;
      $title = $post->post_title;
      if (strlen($title) >= 100 )
        wp_die( "the title must be 100 characters at most" );
      }
    

    You can replace strlen() with str_word_count() if you want to set a word limit instead.

    EDIT: ok with new details you added it seems you could add some jQuery to do the same (strlen)

  2. On a new post, the title is set in <input type="text" name="post_title" size="30" value="" id="title" autocomplete="off">.

    Using a jQuery plugin like http://unwrongest.com/projects/limit/, you could write a WP plugin that targets the post_title and sets the limit right from the editor page.

    You could also add a function in your theme’s functions.php file instead of a plugin, depending on if you want to tie it in to the theme or the site.

    This method makes it possible to keep original length titles if you decided to disable the plugin or change themes.

Comments are closed.