Using WordPress ‘do_shortcode_tag()’ PHP function

The do_shortcode_tag() WordPress PHP function is a regular expression callable used by do_shortcode() to call a shortcode hook.

Usage

To use do_shortcode_tag() function in WordPress, you generally won’t call it directly. It’s used internally by the do_shortcode() function. However, here is a conceptual example of how it might be used:

$regex_match_array = array('sample_shortcode', 'attr1="value1" attr2="value2"', '');
echo do_shortcode_tag($regex_match_array);

In the example, we have a shortcode ‘sample_shortcode’ with two attributes and their corresponding values. The function will process this information and output the result of the corresponding shortcode.

Parameters

  • $m (array): Required. This is the regular expression match array that contains the shortcode to be processed.

More information

See WordPress Developer Resources: do_shortcode_tag()

This function is mostly used internally, and not typically implemented directly by developers. The source code for do_shortcode_tag() can be found in wp-includes/shortcodes.php.

Examples

Basic Usage of do_shortcode_tag

$match_array = array('[gallery]', '', '');
echo do_shortcode_tag($match_array);

In this example, we are processing the ‘gallery’ shortcode, which will display the WordPress gallery if it is registered.

Shortcode with Single Attribute

$match_array = array('', 'id="attachment_12"', '');
echo do_shortcode_tag($match_array);

This example processes the ‘caption’ shortcode with the ‘id’ attribute set to ‘attachment_12’.

Shortcode with Multiple Attributes

$match_array = array('
', 'src="source.mp3" loop="off"', '');
echo do_shortcode_tag($match_array);

In this example, the ‘audio’ shortcode is processed with the ‘src’ attribute set to ‘source.mp3’ and the ‘loop’ attribute set to ‘off’.

Processing a Custom Shortcode

$match_array = array('[custom_shortcode attr="value"]', 'attr="value"', '');
echo do_shortcode_tag($match_array);

Here, a custom shortcode ‘custom_shortcode’ is processed with the ‘attr’ attribute set to ‘value’.

Processing a Complex Shortcode

$match_array = array('[product id="123" size="large" color="blue"]', 'id="123" size="large" color="blue"', '');
echo do_shortcode_tag($match_array);

This example demonstrates processing a more complex shortcode ‘product’ with multiple attributes: ‘id’ set to ‘123’, ‘size’ set to ‘large’, and ‘color’ set to ‘blue’.