Created
December 3, 2014 00:33
-
-
Save taeo/058ac90a522add5c7f3b to your computer and use it in GitHub Desktop.
Wordpress general helpers
This file contains 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 | |
/** | |
Utility funcitons for passing (ahum) "variables" between wp templates | |
**/ | |
$sg_univeral_vars = array(); | |
/** | |
* Add key values that can be retrived w/in other wp templates. | |
* | |
* @uses $sg_univeral_vars | |
* | |
* @param string $k Key for the pair. | |
* @param string $v Value for the pair. | |
*/ | |
function sg_set_universal_var($k, $v) { | |
global $sg_univeral_vars; | |
$sg_univeral_vars[$k] = $v; | |
} | |
/** | |
* Retrieve value set by sg_set_univeral_var | |
* | |
* @uses $sg_univeral_vars | |
* | |
* @param string $k Key for lookup. | |
* @param string $default Fallback value if array key doesn't exist. | |
* | |
* @return bool|string false if a key and default aren't set, string otherwise. | |
*/ | |
function sg_get_universal_var($k, $default = null) { | |
global $sg_univeral_vars; | |
if (isset($sg_univeral_vars[$k])) { | |
return $sg_univeral_vars[$k]; | |
} | |
if ($default) { | |
return $default; | |
} | |
return false; | |
} | |
/** | |
Utility functions to augment wp's body_class() output. | |
**/ | |
$sg_body_classes = array(); | |
/** | |
* Add classes for the body element. | |
* | |
* @uses $sg_body_classes | |
* | |
* @param string|array $class One or more classes to add to the class list. | |
*/ | |
function sg_add_body_class($class = '') { | |
global $sg_body_classes; | |
if (!is_array($class)) { | |
$class = preg_split( '#\s+#', $class ); | |
} | |
foreach ($class as $c) { | |
$sg_body_classes[$c] = $c; | |
} | |
} | |
/** | |
* Remove classes for the body element. | |
* | |
* @uses $sg_body_classes | |
* | |
* @param string|array $class One or more classes to remove from the class list. | |
*/ | |
function sg_remove_body_class($class = '') { | |
global $sg_body_classes; | |
if (!is_array($class)) { | |
$class = preg_split( '#\s+#', $class ); | |
} | |
foreach ($class as $c) { | |
if (isset($sg_body_classes[$c])) { | |
unset($sg_body_classes[$c]); | |
} | |
} | |
} | |
/** | |
* Merge custom classes with wp's classes for the body element as an array. | |
* | |
* @uses $sg_body_classes | |
* | |
* @param array $classes Classes already set by wp. | |
* @return array Array of classes. | |
*/ | |
function sg_merge_body_class($classes) { | |
global $sg_body_classes; | |
$classes = array_merge($classes, $sg_body_classes); | |
return $classes; | |
} | |
add_filter( 'body_class', 'sg_merge_body_class'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment