June 24th, 2010

WordPress Smart Capitalization

Filed under: geek,webdev — saul @ 6:06 AM

function friendlycase($title) {
$title = preg_replace( "/(?< =(?<!:|'s)\W)(A|An|And|At|For|In|Of|On|Or|The|To|With)(?=\W)/e", 'strtolower("$1")', ucwords(strtolower($title)));
return $title;
}
add_filter('the_title', 'friendlycase');

Short sweet and it works… so “HELLO SAUL…THIS IS MOTHRA” becomes “Hello Saul…this is Mothra” particularly handy when someone has a tendency to type in all caps. Painfully simple to add your own exclusionary words..

1 Comment »

  1. SEE? I KNEW IT COULD BE DONE :)

    Comment by Larry here — 06.24.10 @ 8:08 PM

Leave a comment

Comments will be sent to the moderation queue.