Build Your Own Business Website header image

Introduction to Customizing Thesis 2.1 Using PHP – Part 5 – Write our First Function Using a WordPress Filter Hook

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

That was our very first function applied to an action hook in this Customizing Thesis 2.1 using PHP seminar. Now, what we’re going to do is write our very first function using a WordPress Filter Hook.

Difference Between and Action and Filter Hook

A filter hook behaves very much the same here, it’s a function and we’re going to call it hello_world_filter because you can’t have two that are the same name. Now, what’s different between an action hook and a filter hook is that you do show a parameter. It doesn’t matter what you call the parameter but it’s always the content of the filter that you are using.

Using Filter the_content

Let’s get rid of priority here because priority doesn’t really matter that much in this context and the filter hook that we’re going to use is ‘the_content’. Now, you don’t echo the content, you don’t echo it like this, right? What you have to do is you return the value of this parameter back to WordPress and so every filter has to actually have a return statement.

Return Statement

So if we said return ‘Vale Puella’; this would be a correct filter. So this is going to place the value of the content in this and then it’s passing this to the function. The function is returning Vale Puella in the place of ‘the_content’ which means it’s replacing the content with this phrase ‘Vale Puella’.

Upload and View the Site

Let’s hit save and then upload this and then come back over and look at our site. After it’s refreshed, what you’re going to see is the content has been replaced with ‘Vale Puella’. The reason why this content here wasn’t replaced is because it’s not the content, we turned it into the excerpt.

So the excerpt is not being affected by that filter, the content is being affected by the filter. If you look at the content, it has been replaced by ‘Vale Puella’. Every time that filter fires, it is now replaced with ‘Vale Puella’.

Add HTML to the Content

Obviously that’s not particularly useful but you could instead say, return $content and then concatenation symbol and then concatenate ‘Vale Puella’ with this. And in fact, let’s make it an h2 tag so we’re going to add this HTML to the content.

So if we save that and upload it and then come back over and refresh this, now we’ve got the whole content plus we’ve got this h2 that was added to it ‘Vale Puella’. If we look at the HTML of it for a second here, there it is, our content ended right there with that last one and so next up was the h2 tag ‘Vale Puella’.

If you can understand this, that’s how the filter worked. It took the content which starts here and ends here and to that content, it added the HTML that we added and it does it every time the content fires. So here it is at the end of the front page.

Adding Template Tags

Now, that in itself is not particularly useful but what is useful is being able to layer on top of that conditional template tags. I’m not going to talk too much about template tags but you can say, if it’s the front page then return $content plus ‘Vale Puella’ else return $content.

So now in this case, we are asking the question if it’s the front page, then add ‘Vale Puella’ to the content. And if it isn’t the front page, don’t add ‘Vale Puella’ to the content. It’s not so much “don’t”. The right way to say this is, if it’s the front page, return the content plus ‘Valle Puella’, if it’s anything else, just return the content, that’s actually the logic that’s here.

Upload that, refresh it. So here we are on the front page and it added ‘Vale Puella’ but if we go to the menu page, it doesn’t happen. If we go to the blog page, it doesn’t and if we go to a post page, it doesn’t, right? So that’s how that template tag works and that’s how this filter works.

The filter takes a value and allows you to manipulate it, either replace it, process it in some way and return the processed version. It allow you to add stuff to it, put stuff in the beginning, put stuff in the end, anything you can imagine doing to value, you can do using the filter.

0 Comments… add one
0 comments… add one

Leave a Comment