salcode / bootstrap-genesis

WordPress Genesis Child Theme setup to use Bootstrap, Sass, and Grunt
MIT License
184 stars 63 forks source link

Question on Adding classes #130

Open bryanwillis opened 8 years ago

bryanwillis commented 8 years ago

This isn't really an issue but a question I had Sal or someone else that might know. I'm not is the value of the bsg-add-class filter compared to bsg-classes-to-add or the standard genesis_attr_{$context}. Referring to this wiki, these examples below all seem to work the same with the exception of bsg-add-class only able to add one class, so I'm just wondering when to use each?

add_filter( 'bsg-classes-to-add', 'hide_sidebar_on_xs_screen_size', 10, 3 );
function hide_sidebar_on_xs_screen_size( $classes, $context, $attr ) {
    if ( 'sidebar-primary' === $context ) {
        $classes[] = 'hidden-xs';
    }
return $classes;
}
add_filter( 'bsg-add-class', 'hide_sidebar_on_xs_screen_size', 15, 3 );
function hide_sidebar_on_xs_screen_size( $classes, $context, $attr ) {
    if ( 'sidebar-primary' === $context ) {
        $classes[] = 'hidden-xs';
    }
return $classes;
}
add_filter( 'genesis_attr_sidebar-primary', 'themeprefix_add_css_attr' );
function themeprefix_add_css_attr( $attributes ) {
    $attributes['class'] .= ' hidden-xs';
    return $attributes;
}
salcode commented 8 years ago

genesis_attr_sidebar-primary

This is the default Genesis behavior everything else is layered on top. As your notes above indicate, this may be preferable to bsg-add-class.

bsg-add-class

You make an excellent point that there may not be any real value in this filter since the corresponding genesis_attr_ filter can be used instead. We should reconsider example number 2 on https://github.com/salcode/bootstrap-genesis/wiki/Hide-Primary-Sidebar-on-XS-Screen-Size and probably refactor it to use genesis_attr_sidebar-primary instead.

However, even if we modify documentation to recommend against using this filter, I think it makes sense to keep it around.

bsg-classes-to-add

This is the filter I use most often. The default Bootstrap values are assigned here and then these can be modified (as we do in bootstrap-markup-layout-options.php). I find this particularly useful for overwriting (rather than adding to) the default values.

bryanwillis commented 8 years ago

In regard to the custom attributes, here's a handy function I wrote to return the current genesis_attributes to add to the bsg-classes-to-add array.

if( current_user_can('administrator') ) {
    add_action( 'wp_footer', 'bsg_list_genesis_attr_filters' );
}

function bsg_list_genesis_attr_filters() {
    global $wp_filter;
    $genesis_attr_filters = array();
    $output = '<ul>';
    foreach ( $wp_filter as $key => $val ) {
        if ( FALSE !== strpos( $key, 'genesis_attr' ) ) {
            $genesis_attr_filters[$key][] = var_export( $val, TRUE );
        }
    }
    foreach ( $genesis_attr_filters as $name => $attr_vals ) {
                   $name = str_replace("genesis_attr_","",$name);
        $output .= "<li>$name</li>";
    }
    print "$output</ul>";
}

Output

head
body
site-header
site-title
site-description
header-widget-area
breadcrumb
breadcrumb-link-wrap
search-form
nav-primary
nav-secondary
nav-header
nav-link-wrap
nav-link
structural-wrap
content
taxonomy-archive-description
author-archive-description
cpt-archive-description
date-archive-description
blog-template-description
posts-page-description
entry
entry-image
entry-image-widget
entry-image-grid-loop
entry-author
entry-author-link
entry-author-name
entry-time
entry-modified-time
entry-title
entry-content
entry-meta-before-content
entry-meta-after-content
archive-pagination
entry-pagination
adjacent-entry-pagination
comments-pagination
entry-comments
comment
comment-author
comment-author-link
comment-time
comment-time-link
comment-content
author-box
sidebar-primary
sidebar-secondary
site-footer
site-inner
content-sidebar-wrap
nav-footer
widget