Created
December 30, 2011 21:51
-
-
Save trovster/1541636 to your computer and use it in GitHub Desktop.
Custom_Do_Action WordPress class for shortcode hook of functions and do_actions
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 | |
| class Custom_Do_Action { | |
| /** | |
| * __construct() | |
| * @param string | |
| * @param array | |
| */ | |
| public function __construct($options = array()) { | |
| foreach($options as $key => $value) { | |
| $this->$key = $value; | |
| } | |
| add_shortcode('do_action', array($this, 'shortcode')); | |
| return $this; | |
| } | |
| /** | |
| * shortcode() | |
| * @param array $attributes | |
| * @param string $content | |
| * @param string $code | |
| * @param string | |
| */ | |
| public function shortcode($attributes, $content = null, $code = '') { | |
| $content = ''; | |
| $attributes = shortcode_atts(array( | |
| 'function' => '', | |
| 'arguments' => '', | |
| ), $attributes); | |
| ob_start(); | |
| if(!empty($attributes['function'])) { | |
| if(function_exists($attributes['function'])) { | |
| call_user_func_array($attributes['function'], array($attributes['arguments'])); | |
| $content = ob_get_contents(); | |
| } | |
| else { | |
| $content = do_action($attributes['function'], $attributes['arguments']); | |
| } | |
| } | |
| ob_end_clean(); | |
| return $content; | |
| } | |
| } | |
| // [do_action function="get_template_part" arguments="header"] | |
| // [do_action function="get_sidebar" arguments="testimonials"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment