0

A WordPress Filter Example

 3 years ago
source link: https://nixmash.com/on-wordpress/a-wordpress-filter-example/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A WordPress Filter Example – NixMashSkip to content

NixMash

NixMash

WordPress Site Management for Small Business

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.

classfilter1001a.png

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.

classfilter1001b.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK