How to Use Volatyl Filters

Filters are used to alter content before it is sent to or pulled from your database. Between filters and hooks (actions), anything is possible.

What this means to you is that you can change content that is hardcoded into Volatyl without editing Volatyl core files. Awesome.

Volatyl was created with your modifications in mind. So almost everything you see on a Volatyl install can be altered in some way, shape, or form.

To begin using filters, you must first know what content is filterable. Check out the Volatyl Filters List.

Now that you’re familiar, let’s look at how filters get it done.

Understanding Filters

Using the filters is pretty simple. Let’s explain with words instead of codes before we get to the good stuff, though.

What has to happen on Volatyl’s end is certain content must be coded to be changeable.

Once that’s done, custom functions can be written to change those filters. Those function has to accept a variable as a parameter as well as return that variable after it has been modified by the code in your function.

Then, an add_filter must connect your custom function to the filter so that the filter will accept the custom function’s changes to the variable. Get it?

Don’t worry, we’ll actually get our hands dirty.

Using Filters

Based on the description above, the following code should be fairly easy to read. We’re going to change the text that displays before the publish date in a post byline by adding code to our functions.php file.

But first, here’s a description of the content we’re going to filter:

Byline Textbyline_text

This filter controls all static text in the byline (not the links).

Now paste the following code into your child theme’s functions.php file:

Remember, we work inside of child themes only!

// Change publish date text
function publish_date_text( $variable ) {
	$variable = "Published on ";
	return $variable;
}
add_filter( 'publish_date', 'publish_date_text' );

What this code does is create a function called “publish_date_text” that passes variable called $variable. Then, we set that variable equal to something… whatever we want our new text to be for the filter. After we’ve set our variable equal to our new value, we return that variable and close the function. Lastly, we just the add_filter function to link our custom function to that filer.

Now, the content of our filter will be changed to the content that we set equal to our variable in the custom function.

Simple, right?

What if you didn’t want any text to show there at all? Easy. Set the variable equal to an empty string like so:

// Change publish date text
function publish_date_text( $variable ) {
	$variable = "";
	return $variable;
}
add_filter( 'publish_date', 'publish_date_text' );

Some filters may be a little more complicated than others, though.

For example, the pagination_place filter is not as simple as setting your variable equal to a string. Why? Because the code that outputs the pagination place changes dynamically based on which page you’re on. Here’s a description of this filter:

Pagination Placepagination_place

Controls static text before the pagination links. In the basic pagination example “Page 5 of 8 « ‹ 4 5 6 › »”, this filter can change “Page 5 of 8.”

So instead using a basic function like the one before, you have to search for and target the static text you want to change specifically. We’ll do that by setting our variable equal to a str_replace() PHP function that accepts the text we want to change, what we want to change it to, and our variable holding this information as parameters.

// Change pagination place text
function pagination_place_text( $variable ) {
	$variable = str_replace( "Page", "Article", $variable );
	return $variable;
}
add_filter( 'pagination_place', 'pagination_place_text' );

Here, we’ve changed “Page 5 of 8” to “Article 5 of 8” with our function. Remember that we only altered the word “Page” and not the rest of the content.

The string “of” could have been replaced as well using arrays. Again, this is where things get a little more complicated but it’s still pretty simple.

We’ll set one variable (not the same variable being passed through your function) equal to an array holding all of the text we’re replacing.

$search = array( "Page", "of" );

Next, we’ll create another variable and set it equal to an array holding all of the values that the original text will be replaced with. The array keys (each item in the array) from both variables must have corresponding placement based on what to find and what to replace.

$replace = array( "You're on page", "out of" );

Based on the code above, “Page” will be found and replaced with “You’re on page” and “of” will be found and replaced with “out of”.

Now with our search and replace variables set, we need to use those variables in the str_replace() function in the same fashion as before.

$variable = str_replace( $search, $replace, $variable );

Here’s the overall code to change all of the static text in the pagination_place filter.

// Change pagination place text
function pagination_place_text( $variable ) {
	$search = array( "Page", "of" ); // Looking for?
	$replace = array( "You're on page", "out of" ); // Replacing with?
	$variable = str_replace( $search, $replace, $variable ); // Do it.
	return $variable; 
}
add_filter( 'pagination_place', 'pagination_place_text' );

It’s that simple, folks. Use filters to customize your site down to the last character. That’s what Volatyl was created for.

Happy coding! 🙂

Return to docs