How to Set a Character Limit for Post Titles in WordPress

0
43
How to Set a Character Limit for Post Titles in WordPress

How to Set a Character Limit for Post Titles in WordPress

It can be difficult to manage how titles are created if you have several authors contributing work. The last thing you want to do is spend hours going over every post to make sure the titles are compliant. Limiting characters beforehand could save you a great deal of work.

First thing you need to do is install and activate the Limit Post Titles plugin. Upon activation, simply go to Settings » Limit Post Titles to configure the plugin settings.

You need to enter the character limit and select the post types where you want to enable this character limit. Next, click on the save settings button to store your settings.

You can now create a new post and start typing a post title. You will notice that this plugin will show your character limit and let you know when you exceed it.

How to add character limit to posts title in WordPress with a function

nother simple solution is to use a php function. Open functions.php in your theme and paste following code:

function sow_max_title_length( $title ) {
$max = 52;
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). " …";
} else {
return $title;
}
}

Place the next function wherever you want in your theme. For example, if you want to show shortened titles on your homepage, you want to place this code into your Main Index Template (index.php).

add_filter( 'the_title', 'sow_max_title_length');

As you can see in the code, variable $max is used to limit the length of your post title. You are free to change it to any number you want – be aware that the optimal length of your title is between 50 and 60 characters.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments