Last active
February 13, 2020 04:05
-
-
Save bryanwillis/0f22c3ddb0d0b9453ad0 to your computer and use it in GitHub Desktop.
Easily Modify genesis theme framework classes on the fly using genesis_attr();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds Filters Automatically from Array Keys | |
*/ | |
add_action('genesis_meta', 'bw_add_array_filters_genesis_attr'); | |
function bw_add_array_filters_genesis_attr() | |
{ | |
$filters = bw_merge_genesis_attr_classes(); | |
foreach(array_keys($filters) as $context) { | |
$context = "genesis_attr_$context"; | |
add_filter($context, 'bw_add_markup_sanitize_classes', 10, 2); | |
} | |
} | |
/** | |
* Clean classes output | |
*/ | |
function bw_add_markup_sanitize_classes($attr, $context) | |
{ | |
$classes = array(); | |
if (has_filter('bw_clean_classes_output')) { | |
$classes = apply_filters('bw_clean_classes_output', $classes, $context, $attr); | |
} | |
$value = isset($classes[$context]) ? $classes[$context] : array(); | |
if (is_array($value)) { | |
$classes_array = $value; | |
} | |
else { | |
$classes_array = explode(' ', (string)$value); | |
} | |
$classes_array = array_map('sanitize_html_class', $classes_array); | |
$attr['class'].= ' ' . implode(' ', $classes_array); | |
return $attr; | |
} | |
/** | |
* Default array of classes to add | |
*/ | |
function bw_merge_genesis_attr_classes() | |
{ | |
$classes = array( | |
'content-sidebar-wrap' => 'row', | |
'content' => 'col-xs-12 col-sm-8 col-lg-7 col-lg-offset-1', | |
'sidebar-primary' => 'hidden-xs col-sm-4 col-lg-3', | |
'archive-pagination' => 'clearfix', | |
'entry-content' => 'clearfix', | |
'entry-pagination' => 'clearfix', | |
'sidebar-secondary' => '', | |
); | |
if (has_filter('bw_add_classes')) { | |
$classes = apply_filters('bw_add_classes', $classes); | |
} | |
return $classes; | |
} | |
/** | |
* Adds classes array to bsg_add_markup_class() for cleaning | |
*/ | |
add_filter('bw_clean_classes_output', 'bw_modify_classes_based_on_extras', 10, 3); | |
function bw_modify_classes_based_on_extras($classes, $context, $attr) | |
{ | |
$classes = bw_merge_genesis_attr_classes($classes); | |
return $classes; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: New Classes Template | |
* Description: Example modifying page classes on the fly | |
*/ | |
//* Custom classes on the fly like in a page template | |
add_action('genesis_meta', function(){ | |
add_filter('bw_add_classes', 'bw_custom_example'); | |
}); | |
function bw_custom_example($classes) { | |
$new_classes = array( | |
'nav-secondary' => 'navbar navbar-inverse navbar-static-top nav-blue', | |
'entry' => 'panel panel-default', | |
); | |
return wp_parse_args($new_classes, $classes); | |
} | |
genesis(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Modify Layouts | |
*/ | |
add_filter('bw_clean_classes_output', 'bw_modify_classes_based_on_template', 11, 3); | |
function bw_modify_classes_based_on_template( $classes, $context, $attr ) { | |
$classes = bw_layout_options_modify_classes_to_add( $classes ); | |
return $classes; | |
} | |
function bw_layout_options_modify_classes_to_add($classes) { | |
$layout = genesis_site_layout(); | |
// full-width-content | |
if ( 'full-width-content' === $layout ) { | |
$classes['content'] = 'col-sm-12'; | |
} | |
// sidebar-content | |
if ( 'sidebar-content' === $layout ) { | |
$classes['content'] = 'col-sm-12 col-md-8 col-lg-9 col-md-push-4 col-lg-push-3'; | |
$classes['sidebar-primary'] = 'hidden-xs hidden-sm col-md-4 col-lg-3 col-md-pull-8 col-lg-pull-9'; | |
} | |
// content-sidebar-sidebar | |
if ( 'content-sidebar-sidebar' === $layout ) { | |
$classes['content'] = 'col-sm-6'; | |
$classes['sidebar-primary'] = 'col-sm-3'; | |
$classes['sidebar-secondary'] = 'col-sm-3'; | |
} | |
// sidebar-sidebar-content | |
if ( 'sidebar-sidebar-content' === $layout ) { | |
$classes['content'] = 'col-sm-6 col-sm-push-6'; | |
$classes['sidebar-primary'] = 'col-sm-3 col-sm-pull-3'; | |
$classes['sidebar-secondary'] = 'col-sm-3 col-sm-pull-9'; | |
} | |
// sidebar-content-sidebar | |
if ( 'sidebar-content-sidebar' === $layout ) { | |
$classes['content'] = 'col-sm-6 col-sm-push-3'; | |
$classes['sidebar-primary'] = 'col-sm-3 col-sm-push-3'; | |
$classes['sidebar-secondary'] = 'col-sm-3 col-sm-pull-9'; | |
} | |
return $classes; | |
} | |
/* Move sidebar wrap */ | |
remove_action( 'genesis_after_content_sidebar_wrap', 'genesis_get_sidebar_alt' ); | |
add_action( 'genesis_after_content', 'genesis_get_sidebar_alt' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this debug function to print all known page attributes
https://gist.github.com/bryanwillis/16f1755f07e507f2b3b6