WordPress at a glance

add_shortcode() WP 1.0

Adds a new shortcode.

Care should be taken through prefixing or other means to ensure that the shortcode tag being added is unique and will not conflict with other, already-added shortcode tags. In the event of a duplicated tag, the tag loaded last will take precedence.

No Hooks.


null. Nothing.


add_shortcode( $tag, $callback );
$tag(string) (required)
Shortcode tag to be searched in post content.
$callback(callable) (required)
The callback function to run when the shortcode is found. Every shortcode callback is passed three parameters by default, including an array of attributes ($atts), the shortcode content or null if not set ($content), and finally the shortcode tag itself ($shortcode_tag), in that order.


  • Global. Array. $shortcode_tags


Since 2.5.0 Introduced.

Code of add_shortcode() WP 5.7.2

function add_shortcode( $tag, $callback ) {
	global $shortcode_tags;

	if ( '' === trim( $tag ) ) {
		$message = __( 'Invalid shortcode name: Empty name given.' );
		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );

	if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
		/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
		$message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );

	$shortcode_tags[ $tag ] = $callback;

From category: Shortcodes

No comments
    Log In