media - How to limit characters of the post title?

admin2025-06-04  5

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.

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 asked Jul 22, 2013 at 15:19 annabwashereannabwashere 1114 silver badges16 bronze badges 1
  • I'd like to get the character count in real time to help me craft the title (for tweetability, etc) as I type. – annabwashere Commented Jul 23, 2013 at 9:08
Add a comment  | 

3 Answers 3

Reset to default 3

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)

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/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.

I tried with this plugin, It's worked for me.

https://wordpress/plugins/limit-post-titles/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749008183a315572.html

最新回复(0)