Ok, I’ve got this simple code for a simple plugin which prepends post titles with the word TESTING.. I’m really unclear how the parameter works here. I understand how parameters work in PHP, but nothing has been passed inside this parameter for WordPress to know the variable is the title.. if that makes sense?? Don’t get how it’s working.

I’m still fairly crap at WordPress development – I always seem to start, get stuck and don’t get very far.

It only does 1 thing. It takes what it is given, ‘$text’, and returns that $text with the words ‘Testing ‘ prepended to it.

How does WordPress know what ‘$text’ is?

It knows/assigns data to the $text parameter because of this WP filter:

add_filter('the_title', 'cm_title');

This filter is checked by WP when it is building ‘the_title’. So just before WP spits out ‘the_title’ it checks to see if there is a filter to use. In this case we are telling WP to apply this filter (the function ‘cm_title’) before spitting out the title.

All that to say WP sends what it has so far for ‘the_title’ (ie. ‘This is a Post About me’) and runs it through this filter before spitting it out.

So in this case, ‘This is a Post About me’ is the data in the $text parameter in the cm_title function.

Your end result becomes ‘TESTING This is a Post About me’.

Does that help clear it up?

WordPress uses hooks to help developers do what they need to do in a relatively easy way. Some of these hooks are apply_filter() and do_action() which is defined by WordPress. Let’s see exactly what happens when we call the_title() all the way up until it hits the hook you defined.

You’ll notice directly at the bottom of the function it calls apply_filters() and passes in a function_name as string, $title as string and $id as int then returns whatever apply_fitlers() returns, which is your hook. Let’s take a look at apply_filters():

It looks complex but before we jump into that let’s get to the most important piece of this function, $wp_filter. Now this little bugger contains ALL the filters added by both you AND WordPress. You can print it out right underneath your wp_head() to see the full list. It looks something like this ( tons more, I just picked the_title since it’s on topic! )

So it’s a multi-dimensional array of filters, indexed by tag, then by priority and finally by function name. Pretty neat I think! The filter you added, since you didn’t pass a priority, is defaulted to index 10 and would be added right underneath trim and then further indexed by function name cm_title.

Let’s go back a little bit. If we look at the end of get_the_title() function above we see it passing three variables into the apply_filters function but as we can clearly see it only accepts two parameters! I had to look into this a little bit and it looks like this little function, func_get_args() we see littered inside apply_filters() actually will grab anything and everything passed to it. Here’s an example, the Codepen is also nice. Now even though anything can be passed, if we look back up at our mutlidimensional array there’s an accepted_args parameters so technically, you kinda can’t even though it allows you to.

PHEW

Finally, it loops through each one of these filters using the $wp_filters array:

Really, this is called before the_title() ever gets called. Your functions are loaded with wp_head() and thus any filters you add through plugins or functions.php will also be added to this global array.

That was lengthy but hopefully it makes a little more sense now, if you have any questions ask in the comments!