Search code examples
wordpresswordpress-theminggenesis

How to Add Multiple Filters in Function.php?


I'm Using Genesis Framework on my website. I wanted to add Custom CSS Classes to my website's Navigation Menu and the Primary Sidebar.

To make this happen, I used the following code:

add_filter( 'genesis_attr_nav-primary', 'themeprefix_add_css_attr' );
function themeprefix_add_css_attr( $attributes ) {

 $attributes['class'] .= ' toggle';

 return $attributes;

}

And a new class named "toggle" was added to Navigation Primary.

But When I added the code in Function.php for adding a new CSS class in Primary Sidebar as following, my website showed error 500.

add_filter( 'genesis_attr_sidebar-primary', 'themeprefix_add_css_attr' );
function themeprefix_add_css_attr( $attributes ) {

 $attributes['class'] .= 'toggle2';

 return $attributes;

}

Solution

  • Looks like you're defining two functions with the same name, which will cause an error. Try the following:

    add_filter( 'genesis_attr_nav-primary', 'themeprefix_add_nav_css' );
    function themeprefix_add_nav_css( $attributes ) {
    
     $attributes['class'] .= ' toggle';
    
     return $attributes;
    
    }
    
    
    add_filter( 'genesis_attr_sidebar-primary', 'themeprefix_add_sidebar_css' );
    function themeprefix_add_sidebar_css( $attributes ) {
    
     $attributes['class'] .= 'toggle2';
    
     return $attributes;
    
    }
    

    Note that each filter is referencing a different function: themeprefix_add_nav_css and themeprefix_add_sidebar_css.