php - Add class to a div in header from template file in Wordpress with filter
one text
I have a div in header.php like this:
<div id="myDiv">Some text</div>
I want to add a class to that div from a template/template-part file based on data/logic i have in the template file. I tried creating filter like this:
add_filter( 'toggler_class', 'my_class_add',10, 1 );
function my_class_add($class='') {
echo 'class="'.$class.'"';
}
And in header.php i had:
<div id="myDiv" <?php apply_filters('toggler_class', ''); ?> >Some text</div>
Which prints:
<div id="myDiv" class="">Some text</div>
This works; but now I want to modify that filter in a template file so that the div gets a new class:
<div id="myDiv" class="myNewClass">Some text</div>
How can I achieve this?
Source