Last active
March 17, 2016 20:02
-
-
Save strrife/53d3dbf22ece824ae568 to your computer and use it in GitHub Desktop.
Call custom function from shortcode
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
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