Skip to content

Instantly share code, notes, and snippets.

@aaemnnosttv
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaemnnosttv/0751dea2121db3a859fa to your computer and use it in GitHub Desktop.
Save aaemnnosttv/0751dea2121db3a859fa to your computer and use it in GitHub Desktop.
Call Shortcode
<?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