Automatically Title Casing Titles in WordPress

If you’re anything like me, you’re a little lazy when it comes to title casing text in WordPress but as we all know, it’s really important to be consistent across your web pages, so what’s the answer? As I often comment … it’s time for technology to come to the rescue.

Using a simple code snippet I came across on Camen Design, you can easily add a small function to your website, which in turn will automatically convert all your headers to a consistent format.

Start by inserting the following code in your header.php file:

function titleCase ($s_title) {
	//remove HTML, storing it for later
	//         html elements to ignore  | tags  | entities
	$regex = '/<(code|var)[^>]*>.*?</1>|<[^>]+>|&[^w]+;/';
	preg_match_all ($regex, $s_title, $html, PREG_OFFSET_CAPTURE);
	$result = preg_replace ($regex, '', $s_title);

	//break by punctuation, find the start of words
	preg_match_all ('/[w&`'‘’"“.@:/{([<>_]+-? */', $result, $matches, PREG_OFFSET_CAPTURE);
	foreach ($matches[0] as &$m) {
		//find words that should be lowercase
		if ($m[1]>0 && mb_substr ($result, $m[1]-2, 1) !== ':' && preg_match (
			'/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?.?|via)[ -]/i', $m[0]
		)) {
			$m[0] = mb_strtolower ($m[0]);

		//brackets and other wrappers
		} elseif (preg_match ('/['"_{([]/', mb_substr ($result, $m[1], 3))) {
			$m[0] = mb_substr ($m[0], 0, 1).mb_strtoupper (mb_substr ($m[0], 1, 1)).
				mb_substr ($m[0], 2)
			;

		//both of these cases are no change, thus if not matched fall back to capitalisation
		} elseif (!(
			preg_match ('/[A-Z]+|&|[w]+[._][w]+/', mb_substr ($m[0], 1)) ||
			preg_match ('/[])}]/', mb_substr ($result, $m[1]-1, 3))
		)) {
			$m[0] = mb_strtoupper (mb_substr ($m[0], 0, 1)).mb_substr ($m[0], 1);
		}
		//substitute the change into the title
		$result = substr_replace ($result, $m[0], $m[1], strlen ($m[0]));
	}
	//restore the HTML
	foreach ($html[0] as $tag) $result = substr_replace ($result, $tag[0], $tag[1], 0);
	return $result;
}

Next, you’ll need to do a search and replace your entire WordPress theme directory for the current title tag (<?php the_title(); ?>) and replace it with the new tag (<?php echo titleCase(the_title(”,”,FALSE)); ?>). As always, be sure to backup your code before doing this. What the new code has done is told WordPress to insert the title as variable instead of displaying it. Once stored in memory, the titleCase function is called and does it’s job of standardizing the text.

I found a few other really good resources for title casing in PHP in SitePoint and WeberDev but the PHP resource for UC words was one of the best, albeit a little technical, with a little bit of PHP knowledge you should be able to modify the code to work with most CMS applications. Make sure to read about speeding up WordPress however, as this function will certainly add to the server load of a popular website.

8 Responses to “Automatically Title Casing Titles in WordPress”

  1. Donace says:

    I always assumed All in one SEO etc did this automatically? (or so it looked like to me).

    btw how would one subscribe to comments for a post? (hint there is a plugin that can help if i’m not blind and can’t see :p).

  2. Electric cars says:

    It’s pretty easy to write a non-clever title-casing function. The simplest way is to just capitalize the first letter of every word. That’s not right, though, because it’ll leave you with capitalized small words like if,in , of, on, etc. What you want is something that not only knows not to capitalize such words, but will un-capitalize them if they’re erroneously capitalizedin the input.

  3. rv for sale says:

    You seem to be very good with this and its working great. I need to outsource a blog build for a friend, very limited budget but guess you can help us.

  4. Thanks guys, you’re right it’s not the complexity of the code that impressed me here, it’s the idea that now my website has a consistent and uniformed format for all the titles.

    I often tell my son, consistency equals quality. It’s from the McDonalds school of business.

  5. forex trading online says:

    What exactly is the importance of title casing? I have never even heard of it. I guess that makes me sound like a bit of a nube. Some clarification please? PS. I love you blog. Dugg it, and will be back for more.

  6. Hi David, there’s no importance in title casing at all really or at least not from an SEO point of view, my choice to do it is entirely based on creating consistency across the website.

    Instead of one page title reading “How to buy fish” and the next “How to Buy Fish”, I use a script to force all titles across my website to be the same. It’s an interesting script that cuts a lot of the QA time out of my schedule.

  7. well that’s effective, know of an easy way of title casing static sites, other than text-transform:capitalize; and java-script? could be a j-query function haven’t checked?

  8. jason kenny says:

    Nice writing. You are on my RSS reader now so I can read more from you down the road.
    Jason Kenny

Leave a Reply