-
-
Save tolkadot/c3ebb1201b84122056b36bc758c4588f to your computer and use it in GitHub Desktop.
Genesis WordPress Framework adding custom classes to markup This gist was originally created as an example of what I perceived to be an inconsistent behavior, my error was failing to attach my code to action `genesis_setup`. Corrected version now appears below. 20131027 - merged Gary Jones's fork with corrections and refactoring
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 | |
/* | |
* Examples to add custom classes to Genesis WordPress Framework Markup when using HTML5 output | |
*/ | |
add_action( 'genesis_setup', 'srf_add_cust_classes', 15 ); // Priority 15 ensures it runs after Genesis itself has setup. | |
function srf_add_cust_classes() { | |
add_filter( 'genesis_attr_site-inner', 'srf_attr_site_inner' ); | |
add_filter( 'genesis_attr_content-sidebar-wrap', 'srf_attr_content_sidebar_wrap' ); | |
add_filter( 'genesis_attr_content', 'srf_attr_content' ); | |
add_filter( 'genesis_attr_sidebar-primary', 'srf_attr_sidebar_primary' ); | |
} // Don't add a closing marker comment here - it just clutters the code | |
// Don't nest functions, move them outside of the hooked in function. While nested functions work, if the outer function is called again for whatever reason, PHP will throw a wobbly when it tries to redefine an existing function. | |
// Just for fun, I've refactored the common code into one function, and improved it with sanitization. | |
function srf_add_class( $attr, $class ) { | |
$attr['class'] .= ' ' . sanitize_html_class( $class ); | |
return $attr; | |
} | |
// Now the rest of the functions are one line each, and the name of the called function tells us what's happening (add a class). | |
function srf_attr_site_inner( $attr ) { | |
return srf_add_class( $attr, 'example-class-1' ); | |
} | |
function srf_attr_content_sidebar_wrap( $attr ) { | |
return srf_add_class( $attr, 'example-class-2' ); | |
} | |
function srf_attr_content( $attr ) { | |
return srf_add_class( $attr, 'example-class-3' ); | |
} | |
function srf_attr_sidebar_primary( $attr ) { | |
return srf_add_class( $attr, 'example-class-4' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment