Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
I'd like to get the character count in real time to help me craft the title (for tweetability, etc) as I type. –  annabwashere Jul 23 '13 at 9:08

2 Answers 2

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)

share|improve this answer
    
I forgot to mention that I'm looking to get the character count in real time. I'll update my original question with that detail now. –  annabwashere Jul 23 '13 at 9:07

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.

share|improve this answer
    
Thanks! I'll see if I can get this working. –  annabwashere Jul 23 '13 at 9:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.