Last active
August 29, 2015 14:20
-
-
Save aaemnnosttv/0751dea2121db3a859fa to your computer and use it in GitHub Desktop.
Call 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
<?php | |
if ( ! function_exists('call_shortcode') ) : | |
/** | |
* Helper function for calling a shortcode directly | |
* | |
* Rather than `echo do_shortcode('[foo bar=the]')` ... | |
* | |
* echo call_shortcode('foo', ['bar' => 'the']); | |
* | |
* @param string $tag the shortcode [tag] | |
* @param mixed $atts array or string of shortcode attributes | |
* @param string $content inner content of shortcode | |
* @return mixed string - returned output from shortcode | |
* null if shortcode callback does not exist or is not callable | |
*/ | |
function call_shortcode( $tag, $atts = [ ], $content = '' ) | |
{ | |
global $shortcode_tags; | |
if ( empty( $shortcode_tags[ $tag ] ) || ! is_callable( $shortcode_tags[ $tag ] ) ) { | |
trigger_error( __FUNCTION__ . ": shortcode callback for [$tag] is not callable!" ); | |
return null; | |
} | |
if ( is_string( $atts ) ) { | |
$atts = (array) shortcode_parse_atts( $atts ); | |
} | |
return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag ); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment