A WordPress Filter Example

Our Series Twenty Seventeen continues with an example of using WordPress Filters.

The Objective: In the previous post of our WordPress Series we removed the Twenty Seventeen Sidebar from the Single Post Page. To do that, we need to add a unique “nixmash-single-page” class to the HTML <body> tag, which we will do using WordPress Filters.

Here’s a description of WordPress Filters, taken from WPBeginners.

A Filter is a function that can be hooked to an event in WordPress (called hooks). During the execution when the event is triggered the filter is applied to the data output generated by the event hook.

We’ll be using the body_class filter, which is applied to the classes for the HTML <body> element. Here’s the WordPress API/Filter Reference on filters for more info.

If you recall, one of the rules of Child Themes is that functions.php is executed after the Parent Theme function.php. Also, any Child Theme PHP file REPLACES the Parent Theme PHP. We’re doing a PHP require of /inc/template-functions.php in our functions.php so we get both Parent and Child template functions.

require get_theme_file_path('/inc/template-functions.php' );

Now we can hook our nixmash_body_classes() function to the body_class filter.

The Win: A new “nixmash-single-page” class is now added to the <body> tag that we can use in our CSS to override the Parent Twenty Seventeen Theme stylings and expand the width of our post.