Extending the functionality of your WordPress website is as simple as installing a good plugin. But what if you need a custom functionality not available by plugin? With a little understanding of PHP and WordPress actions and filters (wait don’t leave me yet!) you too can add helpful features to your own WordPress site. In fact, one of the first plugins I wrote was to solve a problem for my employer for which there was not extant plugin.

Let’s take a look at a simple WordPress function present in every installation of WordPress since version 3.0. A function is a bit of PHP programming that can be executed whenever we like and allow you to do something (hopefully) useful.

The function you ask? capital_P_dangit() . The sole purpose of this function is to capitalize the letter ‘p’ in Wordpress. Notice it didn’t work there eh? Take that capital_P_dangit()#!

Okay, all attempts at some programming humor aside, here is the function:

[code]function capital_P_dangit( $text ) {

// Simple replacement for titles

if ( ‘the_title’ === current_filter() )

return str_replace( ‘WordPress’, ‘WordPress’, $text );

// Still here? Use the more judicious replacement

static $dblq = false;

if ( false === $dblq )

$dblq = _x(‘“’, ‘opening curly quote’);

return str_replace(

array( ‘ WordPress’, ‘‘Wordpress’, $dblq . ‘WordPress’, ‘>Wordpress’, ‘(WordPress’ ),

array( ‘ WordPress’, ‘‘WordPress’, $dblq . ‘WordPress’, ‘>WordPress’, ‘(WordPress’ ),

$text );

}[/code]

As I mentioned, the whole point of this plugin is to capitalize the letter ‘p’ in the phrase “WordPress”. The above function can be broken down into simpler parts. Let’s look at just the first three lines:

The first line, function capital_P_dangit( $text ) { sets the stage for this useful bit of code. The term ‘function’ lets the PHP interpreter know this is the beginning of a … well, function. The next phrase ‘capital_P_dangit’ is the custom name of this function and can be used elsewhere in the code base to execute the code contained therein. And the parenthesis and text within are referred to as arguments and are processed from left to right. So this gives us a PHP function called capital_P_dangit(). Now on to what it does.

The second line, if ( 'the_title' === current_filter() ) , in English terms, means if the currently active filter is the WordPress filter ‘the_title’, then execute the following code. This will affect instances of the content title from the post editor.

The third line, return str_replace( 'Wordpress', 'WordPress', $text ); , is where the rubber meets the road. In normal parlance, it is looking for instances of ‘WordPress’ with a non-capitalized ‘p’. It would then replace that incorrect version with the correct version.

The remaining lines of the function perform the same basic tasks in other content areas of the WordPress posts, pages, etc.

Now we are going to write our own fun function. Functions typically are placed in your theme’s functions.php file or in the context of a plugin. So let’s write a plugin to change every instance of the word “monkey” in a WordPress title with this: MONKEY. Let’s do it!

First, try this on your own then go to the next page to see how your result compares to mine.