Skip to content

Instantly share code, notes, and snippets.

@trovster
Created December 30, 2011 21:51
Show Gist options
  • Select an option

  • Save trovster/1541636 to your computer and use it in GitHub Desktop.

Select an option

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
<?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