WordPress – Changing the on page title without renaming the page

From working with an in house SEO team, I regularly get asked about changing particular sections of text within WordPress sites, the answer usually lies within an excerpt, a widget or a custom field. In the past, one of the more troublesome snippets has been the on page title – this is usually a h1 or h2 tag near the top of the page, just above the content.

Having the right page title is key to both users and search engines. SEO’s for example will usually want to change this title to gain every ounce of value from the pages content. Other users may want the title to say something like “Learn more about our products” as opposed to simply “Products”.

The Problem

The fact that this title is generally called dynamically by WordPress means changing it can have adverse affects elsewhere if it’s not done carefully.

What could go wrong?

  • The page URL can change – potentially breaking existing incoming links to the page
  • The label for its menu item can be changed – messing up your wonderfully crafted navigation
  • The page name in the admin screen will be changed, making it inconsistent or confusing
  • Dynamic page lists throughout your site can be modified

For an SEO, a web developer or a hardened WordPress veteran there is usually a straight forward workaround to these problems……but there shouldn’t have to be.

The Solution

A solution can by applied with a tiny php function and a simple custom field.

The Function

Add the following to your functions.php file

function customTitle(){
global $post;
$customTitle = get_post_meta($post->ID, ‘custom_on_page_title’, true);
if(!$customTitle){
the_title();
}else{
echo $customTitle;
}
}

What will this do?

This function will search the page for a custom field called custom_on_page_title which contains your custom title, if it doesn’t find one, it will simply display the default WordPress title.

The Custom Field

On whichever page needs a custom title, create a custom field with the name custom_on_page_title and set the value to whatever your title should be.

Showing it in your theme

The next step is to add it to your template file

  • Open the template you would like to edit
  • Find where the_title() is being called
  • Replace the_title() with customTitle()

And you’re done!

I like this useful little technique because it adds a bit more flexibility without compromising any of WordPress’s native functionality.