Skip to content

Instantly share code, notes, and snippets.

@yumyo
Last active January 11, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumyo/143c0159c5bfde6ab342 to your computer and use it in GitHub Desktop.
Save yumyo/143c0159c5bfde6ab342 to your computer and use it in GitHub Desktop.
WordPress FEATURE BASED ROUTING - Example based on Sage but could be extended / adapted to any WP or HTML theme
<body <?php body_class(); ?> data-features="<?php echo Roots\Sage\Extras\get_feature(); ?>">
function get_feature() {
if (is_single()) {
$feature = 'feature_one';
} elseif(is_page() && !is_front_page()) {
$feature = 'feature_two';
} elseif(is_front_page()) {
$feature = 'feature_three';
} else {
$feature = '';
}
return $feature;
}
add_action( 'wp_head', __NAMESPACE__ . '\\get_feature' );
SITE = {};
var body = document.body;
SITE.features = {
init: function() {
var features = body.getAttribute( "data-features" );
var featuresArray = [];
this.common.init();
if(features) {
featuresArray = features.split(' ');
for(var x = 0, length = featuresArray.length; x < length; x++) {
var func = featuresArray[x];
if(this[func] && typeof this[func].init === 'function') {
this[func].init();
}
}
}
},
common: {
init: function() {
}
},
feature_one: {
init: function() {
}
},
feature_two: {
init: function() {
}
};
SITE.features.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment