Alternate Title – WordPress

By Lew Ayotte  |  July 9th, 2009  |  Published in WordPress, development  |  2 Comments

We’ve been working on a site where the client wanted the page title to be different on the front end and the back end. They want a longer more descriptive title on the back end. On the front end, they wanted to customize the title for their customers to see. Basically something small and simple.

Immediately I knew that using WordPress Custom Tags would work perfectly for this. So we went with the meta-tag “alt_title” for their alternate titles. On my first attempt I edited the relevant .php theme files to do this for the title:

<?php
if ($p = get_post_meta($post->ID, "alt_title", true)) {
	$title = $p;
} else {
	$title = get_the_title($post->ID);
} 
?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title='Click to read: "<?php echo $title; ?>"'>
<?php echo $title; ?></a></h2>

However, that wasn’t quite good enough. They also wanted to be able to make an alternate name for page titles in a navigation. In their current navigation, using wp_list_pages, it was displaying the actual page title, not the alternate title. This took a little extra work, but finally, with a pretty simple piece of code. I added this function to the functions.php in their theme.


function the_alt_title($title= '') {
	$page = get_page_by_title($title);

	if ($p = get_post_meta($page->ID, "alt_title", true)) {
		$title = $p;
  	}

	return $title;
}

Now, all I need to do is apply this function to the the_title filter, with:

add_filter('the_title', 'the_alt_title', 10, 1);

But, if you remember, they want to see the original titles on the backend, so I actually needed to write it like this:

if (!is_admin()) {
	add_filter('the_title', 'the_alt_title', 10, 1);
}

I don’t know if there is much need for this functionality, but this could easily be turned into a WordPress plugin. There is still a little more work that would need to be done. But it works for our client’s needs.

As always, your questions and comments are welcome.

Bookmark and Share

Responses

  1. Scott says:

    October 6th, 2009 at 11:34 pm (#)

    I’ve been beating my head trying to figure out how to keep add_filter from being applied to the backend.

    Who know it was as easy as !is_admin?

    Thanks!

  2. Lew Ayotte says:

    October 7th, 2009 at 6:44 am (#)

    Hehe, no problem :) … sometimes it’s the simple things that stump us the easiest!

    Lew

Leave a Response