Skip to content

Instantly share code, notes, and snippets.

@strrife
Last active March 17, 2016 20:02
Show Gist options
  • Save strrife/53d3dbf22ece824ae568 to your computer and use it in GitHub Desktop.
Save strrife/53d3dbf22ece824ae568 to your computer and use it in GitHub Desktop.
Call custom function from shortcode
add_shortcode( 'call_function', 'custom_function_call' );
/**
* @param $attributes
* $attributes['name'] - function name to call
* $attributes['args'] - arguments to be passed
* @return string
*/
function custom_function_call( $attributes ) {
$pattern = false;
$attributes = shortcode_atts( array(
'name' => false,
'args' => '',
), $attributes );
$name = $attributes['name'];
if(
$name &&
function_exists($name) &&
(!$pattern || preg_match($pattern, $name ))
){
$params = array_map('trim', explode(',', $attributes['args']));
ob_start();
echo call_user_func_array($name, $params);
return ob_get_clean();
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment