• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP Bitz

A curated collection of code snippets for WordPress

  • Home
  • About
  • Blog
  • Code Snippets
You are here: Home / Code Snippets / Call a shortcode function by tag name

Call a shortcode function by tag name

December 3, 2019

/**
 * Call a shortcode function by its tag name.
 * 
 * Directly executes a shortcode's callback function using the shortcode's
 * tag name. Can execute a function even if it's in an object class.
 * Simply pass the shortcode's tag and an array of any attributes.
 *
 * @global array  $shortcode_tags
 * @param  string $tag            The shortcode tag name.
 * @param  array  $atts           The attributes (optional).
 * @param  array  $content        The shortcode content (null by default).
 *
 * @return string|bool False on failure, the result of the shortcode on success.
 */
function do_shortcode_func( $tag, array $atts = array(), $content = null ) {
 
    global $shortcode_tags;
 
    if ( ! isset( $shortcode_tags[ $tag ] ) ) {
        return false;
    }
 
    return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}

Chad Butler

Primary Sidebar

Join for free!

Get free code snippets, WordPress best practices, and practical Content Marketing advice from seasoned WordPress expert Chad Butler (butlerblog):

Recent Posts

  • Block an IP address from accessing your site
  • Using WordPress Debug Constants
  • Save WordPress debug log by date
  • Utility function for the WordPress error log
  • How to Create a Site Specific Plugin

Copyright © 2021 · Maker Pro on Genesis Framework · WordPress · Log in