Search:

capitalize page titles in WordPress

(without using a Plugin)

 

While working on a custom WordPress theme for a client this morning, I came across a peculiar problem. The words I was using for custom tags and categories which (in this particular theme design) get displayed in the page’s <title> in breadcrumb-like fashion, were being displayed just as they were typed in – with all lower-case characters. After doing some Google searching, I discovered there was no documented easy solution to capitalizing these text values. I found that there were many Plugins available that do this, but I really felt like installing a Plugin just to get some words capitalized seemed like overkill. Eventually, by using some of the code I found in those plugins, I hand-rolled the simple solution I was looking for, and thought I’d share it with y’all here…

First, let me explain that I did not have an issue with getting some things capitalized, like category name, for instance. To do that i simply used the PHP string function, ucwords() inline like so…

<title>Site Name - <?php echo ucwords($category); ?></title>

The problem I was having was with the WordPress template tags: wp_title and the_title. You can’t use ucwords() directly on these because they’re not a string. So, the solution I came up with was to write a small PHP function at the top of my “header.php” file that takes the text values from wp_title and the_title, turns them into a string, and applies capitalization with the ucwords() function…

<?php
function captitle($title) {
     $title = ucwords($title);
     return $title;
}
add_filter('wp_title', 'captitle');
add_filter('the_title', 'captitle');
?>

 
enjoy.

 

     
  • need my help?

    Interested in having me be a consultant on your next project?  to email me.

  •  
  • what i’m reading...

  • Run Windows on Mac OS X with no reboot!ING DIRECT Orange Savings Account- Apply Today!
    BasecampGo Daddy $6.99.com sale 125x125

    Order DIRECTV Today for $29.99 a month