How to Customize Thesis with Filters

This document is deprecated! The information on this page refers to a Thesis version that is now obsolete. Please visit the Thesis Docs for current documentation.

What are filters? Like hooks, filters are points of interaction within Thesis, and just like with hooks, you need to create and add functions to filters to achieve results.

The difference? Filters operate on content that already exists. This means two things:

  1. a filter function must accept a variable as a parameter, and…
  2. a filter function must return the resulting content after all operations on it have been performed

Before Filter

Before Filter

Let me illustrate with a simple example: I want to change “add one” to “Shout @ Me!” in the introduction to comments. To work on that text, we’ll be using the thesis_comments_intro filter, but first we need to create our function:

function custom_comment_intro($content) {

}

You can name your function just about anything you’d like; the import thing is that you allow a variable to be passed to your function. The name of the variable is irrelevant, so for simplicity, I usually use $content.

The $content variable contains all of the output of thesis_comments_intro(), including not only the “add one” text, but the entire div.comments_intro block. All of that markup is available via our $content variable to modify — we can remove it completely, change all of the words to something contextual on our site, use our own markup rather than Thesis’ default, and so on. Sky’s the limit, just like with hooks!

Okay, back to our function. Let’s take our variable and do something with it:

function custom_comment_intro($content) {
	$content = str_replace('add one', 'Shout @ me!', $content);
}

See what we’ve done there? We’ve taken the original $content and modified it using the str_replace() PHP function. Only two more things we need to do to get our customization up and running, so let’s add them in to the mix:

function custom_comment_intro($content) {
	$content = str_replace('add one', 'Shout @ me!', $content);
	return $content;
}
add_filter('thesis_comments_intro', 'custom_comment_intro');
After Filter

After Filter

What we’ve done is to return the modified content. You’re probably used to echoing content when customizing with hooks; filters, however, are processed prior to echoing anything, so we simply want to return our content so that it can continue to be processed by any other filters or WordPress/Thesis functions which need to be run.

Finally, we add our custom function as a filter on the thesis_comments_intro item.