Can someone help me understand how the filters work? #922
-
I do not understand how to use filters after reading the documentation page about them and was hoping someone could help me. For example, this code:
Is 'bootscore/class/container' the name of a real filter hook? If so, where is 'bootscore/class/container' defined in the parent theme? And this code:
To apply that filter I would use something like apply_filters('bootscore/class/container', 'container') in a template file? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
No, filters are here to change template classes without doing something in the template files, use child-theme's The usage is exactly as described in the docs. However, lets do something crazy to make them more visible in your site. All code goes to your child-theme's This one will change all /**
* Change entire container classes
*/
function container_class() {
return "container-fluid bg-danger border border-5 border-success";
}
add_filter('bootscore/class/container', 'container_class', 10, 2); If you want to change only the container in header, replace snippet: /**
* Change container class in a single file
*/
function container_class($string, $location) {
if ($location == 'header') {
return "container-fluid bg-danger border border-5 border-success";
}
return $string;
}
add_filter('bootscore/class/container', 'container_class', 10, 2); If you want to change the container in multiple files, for example header and footer, use this: /**
* Change container classes in selected files
*/
function container_class($string, $location) {
if ($location == 'header' || $location == 'footer-top' || $location == 'footer-columns' || $location == 'footer-info') {
return "container-fluid bg-danger border border-5 border-success";
}
return $string;
}
add_filter('bootscore/class/container', 'container_class', 10, 2); Check out the examples in the docs page, for what you can do with this filters. Change classes as you want. Solved? |
Beta Was this translation helpful? Give feedback.
-
Hey, thank you for the response. It is making a little more sense now. Is 'bootscore/class/container' the name of a filter hook? It looks like the location of a file. Where does that come from? Would you use apply_filter() to make changes in a template file? I've read the WordPress and Bootscore docs and I am still confused. |
Beta Was this translation helpful? Give feedback.
-
Okay, so it looks like in this code:
you are passing header as the third argument. Thank you again for all your help. Much appreciated. |
Beta Was this translation helpful? Give feedback.
Ok, let's start from scratch. Let's say there is a simple
div
with classes in a template file:If you want to change class
one or more classes
, then you have to copy the entire template file to your child-theme and change the classes directly. Depending on how deep you customise your theme, you will have a lot of copied templates in your child which gets not updated when you update Bootscore.Basic
For this purpose, most classes and other things are wrapped in a filter to change them without storing a copy of a template file in your child. A basic filter has:
bootscore/filter/name
string
, hereone or more classes