How to Customize WordPress Titles

Having a website or a WordPress blog without managing titles is quite impossible. If you have ever forgotten to write a title, you quickly realized that WordPress added one on its own. Since they’re important to admins, other users, visitors, and even search engines, you should take extra caution when writing titles.

While you should definitely spend an extra minute on every title that you write, there are some things you can change on the entire site. So, stay with us through this guide and learn how to modify different title attributes in WordPress.

How to limit WordPress title length

When you want to keep your post titles as short as possible and you work with multiple authors, that might become a problem. Whether you’re starting a fashion blog, food blog or any other type of a website, everyone would have to take care of their titles, count words or letters and it would probably be a hard time doing that every time when you write a new article.

So how do you limit a post title in WordPress so it automatically takes care of the title length being showed on your page?

Let’s start with a simple solution.

  1. Open functions.php in your theme
  2. Paste the following code:
function max_title_length( $title ) {
$max = 20;
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). " …";
} else {
return $title;
}
}



  1. 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', '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. But be aware that the optimal length of your title is between 55 and 60 characters.

There you go. If you have called the function in your Main Index Template, your titles will be displayed shortened and after, in our case 20 characters, three dots will be revealed to show there is more to display.

By doing the same, you are free to hook the function wherever you want it – be it header, footer, sidebar or anything else.

How to change the alignment of your post title

WordPress Themes which you can use in WordPress are different. You can use a standard one that comes with WordPress, create your own, find a free one among thousands of available themes, or purchase a premium one from the Internet.

Even if you have paid big bucks for your theme, it won’t have everything in reach of your hand and it’s impossible to incorporate everything into a user-friendly interface. So, if you want to change the alignment of your post titles and your theme doesn’t come with an easy-to-change option, you will have to do it yourself.

Prepare your tools and let’s loose some screws so you can get the job done. OK, this sounded like there are hours of work before you – don’t worry, you’ll be done in a minute or so.

It doesn’t matter which theme you’re using, a title for your post will be located in the same file:

  1. Navigate to Appearance->Editor
  2. On the right side, find Single Post file (single.php) and open it or even better, open the file in some external editor
  3. Search file for “<?php the_title(); ?>“
  4. Modify the tag with the desired alignment:

LEFT:

<div align="left"><?php the_title(); ?></div>



CENTER:

<div align="center"><?php the_title(); ?></div>



RIGHT:

<div align="right"><?php the_title(); ?></div>



  1. Save changes

That’s it. Now you are free to open any of your posts and see the changes you have made. If you have followed the steps and changed alignment, your post title should appear in the left, center or right.

Working with WordPress is fun, isn’t it? If you want to learn more about blogging and customizing WordPress, see our resources and level up your webmaster skills in no time.

Write a list of words that can’t be used in post titles

While you can relatively easily limit the length of a title in WordPress, sometimes you will need to modify titles even more. Limiting the number of characters in a post title can help you maintain a stable design and it can help with SEO, but your authors would still be able to write anything they want.

Depending on what you write or on your partnership with some other company, you might have some words or phrases that you want to avoid in your titles. Those might be profanity words that you want to avoid or simply a brand name or two that you don’t want to be advertised on your site.

While you can sit and talk with your authors about that and ask them not to mention words which might harm your site’s reputation, it is just the matter of time when one of the authors will forget all about it and publish a title which can make you look bad or even lose money by breaching the deal with a partner.

List of words not to use in titles

Instead of a verbal warning, why wouldn’t you write a function which will forbid anyone on your site to write a title with specific words? Or even better, why wouldn’t you just copy and paste the same function from below and simply change the words which you want to block?

Now that you’ve decided to remove specific words from post titles, let’s see how you can do that.

Remove specific words from titles:

  1. Open functions.php file
  2. Copy and paste the code:
function titlerestriction($title){
global $post;
$title = $post->post_title;
$restrictedWords = "word1;word2;word3";
$restrictedWords = explode(";", $restrictedWords);
foreach($restrictedWords as $restrictedWord){
if (stristr( $title, $restrictedWord))
wp_die( __('Error: You have used a forbidden 
word in post title') );
}
}
add_action('publish_post', 'titlerestriction');



  1. Change words on the 4th Add as many as you like but don’t forget to separate them with a semicolon
  2. Customize the error message on the 8th line
  3. Save changes

If you open a new post and try to publish it while the title contains one of the words you have specified in the code, WordPress will stop you from publishing the post and warn you with the message.

Although you may have told your authors about forbidden words, it wouldn’t hurt to show those words right below the title and write them as a reminder.

If it’s not top-secret, can you tell us which words have you put on the list and why?

A small title separator change can make a big impact on your site

Title separator is that one simple character that separates your website title from post and page names. Although as not important as finding the right keywords for your articles, this can be seen on top of your browser tab once you open a website, but more importantly, this separator will be used by Google and other search engines when they display your site in search results.

While there is no proof that different separators can have a different impact on your SEO, you might want to change the standard separator just in order to change the way your WordPress website is being displayed on all the other sites in those search results.

Change title separator:

After WordPress 4.4 came to life, there were new filters introduced, and one of them is directly in charge of title separators. In order to change your title separator, follow these few steps:

  1. Open functions.php file
  2. Copy and paste the following code:
function wploop_change_separator()
{
return '|';
}
add_filter('document_title_separator', 
'wploop_change_separator');



  1. Change the separator in between single quotes on the third line
  2. Save changes

Change title separator for older versions of WordPress

Before WordPress 4.4 was introduced, title separator could have been easily changed by using the wp_title function. WordPress developers first removed the function from the list of supported ones and it became deprecated. But since a huge number of themes are still using it, developers decided to bring it back.

The following function can still help you change that separator if you’re using older WordPress version, but we advise that you update your WordPress as soon as possible (for many different reasons) and go with the new method shown above; sooner or later, wp_title function will become deprecated once and for all and you will have to modify functions.php once again.

function change_wp_title_separator( $title, $sep ) {
$sep = '-';
$title = str_replace( '|', $sep, $title );
return $title;
}
add_filter( 'wp_title', 'change_wp_title_separator', 
10, 2 );



While you can change the separator to any character you want, we advise not to do so. For example, there are some special characters that Google simply won’t show and you don’t want your site title to look unprofessional. Some of the most common separators are “|”, “-“ and “>” so don’t exaggerate and choose the one you like the best.

Title tags are important for your website. Not only they will show up to users, but they will take an important role in your SEO. Choosing the right title tag can have a big impact on search engines and you can benefit from changing title tags if done correctly.

For a regular user, it is easy to change title tags simply by navigating to Settings -> General. Similarly, going to Appearance -> Customize will show a place where almost every theme will allow you to change your site’s title and tagline. Usually, websites use their title and post/page name, separated by one character. But you can change that if you want to be so rebellious.

Create a static title tag for all of your posts:

Although not recommended, you might want to change the page title tag and make it the same on all of your WordPress posts and pages. Instead of using a title, separator and post name, for example, you can change that into something static:

  1. Open functions.php file
  2. Copy and paste the following:
function same_title_tag()
{
return 'First Site Guide';
}
add_filter('pre_get_document_title', 
'same_title_tag');



  1. Change the title on the 3rd line
  2. Save changes

This function would change every post and page title into “First Site Guide” (hey, don’t use our name, be creative), but that’s acceptable if you’re about to have one page only. Instead, using default titles is a much better option when it comes to SEO and overall user experience. Even if you had only home and contact page, it’s still a better option to have that “contact” part written in the title tag.

Change title tags through custom fields:

But, let’s build upon this and give your authors the possibility to add custom titles to their posts:

  1. Open functions.php
  2. Copy and paste the code:
function post_meta_title_tag()
{
$customPostTitleMeta = get_post_meta( get_the_ID(),
'custom_post_title', true);
if($customPostTitleMeta)
{
return $customPostTitleMeta;
}
return '';
}
add_filter('pre_get_document_title', 
'post_meta_title_tag');
  1. Save changes
  2. Open a post for which you want to change the title
  3. In the custom field’s name, write “custom_post_title”
  4. In the custom field’s value, write any title you want to use for that post
  5. Save changes/publish a post

Now, instead of getting a default title, your post would use the custom one you have entered on the 6th step. If you leave out the custom field, WordPress will load default title tag and use it for that post.

Control your WordPress titles

We hope that this guide has helped you control your WordPress titles more easily. But if you have any questions, please feel free to leave and comment and we’ll try to asnwer them as soon as possible.