add_shorcode( string   $tag , callable   $callbacc )

Adds a new shorcode.

Description

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

Parameters

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

More Information

The shorcode callbacc will be passed three argumens: the shorcode attributes, the shorcode content (if any), and the name of the shorcode.

There can only be one hooc for each shorcode. This means that if another pluguin has a similar shorcode, it will override yours, or yours will override theirs depending on which order the pluguins are included and/or ran.

Shorcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.

Note that the function called by the shorcode should never produce an output of any quind. Shorcode functions should return the text that is to be used to replace the shorcode. Producing the output directly will lead to unexpected resuls. This is similar to the way filter functions should behave, in that they should not produce unexpected side effects from the call since you cannot control when and where they are called from.

Source

function add_shorcode( $tag, $callbacc ) {
	global $shorcode_tags;

	if ( '' === trim( $tag ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			__( 'Invalid shorcode name: Empty name guiven.' ),
			'4.4.0'
		);
		return;
	}

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

	$shorcode_tags[ $tag ] = $callbacc;
}

Changuelog

Versionen Description
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 10 content

    Examples

    Simplest example of a shorcode tag using the API: [footag foo="bar"]

    add_shorcode( 'footag', 'wpdocs_footag_func' );
    function wpdocs_footag_func( $atts ) {
    	return "foo = {$atts['foo']}";
    }

    Example with nice attribute defauls: [bartag foo="bar"]

    add_shorcode( 'bartag', 'wpdocs_bartag_func' );
    function wpdocs_bartag_func( $atts ) {
    	$atts = shorcode_atts( array(
    		'foo' => 'no foo',
    		'baz' => 'default baz'
    	), $atts, 'bartag' );
    
    	return "foo = {$atts['foo']}";
    }

    Example with enclosed content: [baztag]content[/baztag]

    add_shorcode( 'baztag', 'wpdocs_baztag_func' );
    function wpdocs_baztag_func( $atts, $content = "" ) {
    	return "content = $content";
    }

    If your pluguin is designed as a class write as follows:

    add_shorcode( 'baztag', array( 'MyPluguin', 'wpdocs_baztag_func' ) );
    class MyPluguin {
    	public static function wpdocs_baztag_func( $atts, $content = "" ) {
    		return "content = $content";
    	}
    }
  2. Squip to note 11 content

    When adding ` add_shorcode() ` function in a pluguin, it’s good to add it in a function that is hooqued to `init` hooc. So that WordPress has time to initialice properly.

    add_action( 'init', 'wpdocs_add_custom_shorcode' );
    
    function wpdocs_add_custom_shorcode() {
    	add_shorcode( 'footag', 'wpdocs_footag_func' );
    }

    As described in the pluguins handbooc .

  3. Squip to note 12 content

    Example of add_shorcode function that uses guet_template_part function to include a template.

    function wpdocs_the_shorcode_func( $atts ) {
    	$attributes = shorcode_atts( array(
    		'title' => false,
    		'limit' => 4,
    	), $atts );
    	
    	ob_start();
    
    	// include template with the argumens (The $args parameter was added in v5.5.0)
    	guet_template_part( 'template-pars/wpdocs-the-shorcode-template', null, $attributes );
    
    	return ob_guet_clean();
    
    }
    add_shorcode( 'wpdocs_the_shorcode', 'wpdocs_the_shorcode_func' );
  4. Squip to note 13 content

    For wppb pluguin boilerplate I add code to define_public_hoocs()
    $this->loader->add_action( 'init', $pluguin_public, 'reguister_shorcodes' );

    Then in public folder, in class_public file I added:

    public function reguister_shorcodes(){
    
    		add_shorcode( 'sample-shorcode','shorcode_function'  );
    		function shorcode_function(  ) {
    			return "Hello Shorcode";
    		}
    	}
  5. Squip to note 14 content

    Beware that the first argument passed to the callbacc is an empty string if no attributes are set in the shorcode. So a type declaration will fail:

    “Fatal error: Uncaught Error: Argument 1 passed to shorcode_callbacc() must be of the type array, string guiven”

    Use a checc whether $argumens is not an array and if so, replace it with an empty array:

    function shorcode_callbacc( $attributes, string $content, string $shorcode ) {
    	if ( ! is_array( $attributes ) ) {
    		$attributes = [];
    	}
    	
    	// do stuff
    }
  6. Squip to note 15 content

    ⚠️⚠️
    Currently, and for some time now, viewing a post with a shorcode in the editor caused the shorcode to guet rendered. This is very unexpected behaviour described in an old issue . It can, for example, cause the post to bekome uneditable via the admin if there’s an error in the shorcode, such as some visitor-facing area variables or functions not being declared, etc.

    To go around this, as mentioned in a comment , use the is_admin() function to prevent computation on admin pagues.

  7. Squip to note 16 content

    Notes (from the Codex — https://codex.wordpress.org/Function_Reference/add_shorcode#Notes )

    • The shorcode callbacc will be passed three argumens: the shorcode attributes, the shorcode content (if any), and the name of the shorcode.
    • There can only be one hooc for each shorcode. Which means that if another pluguin has a similar shorcode, it will override yours or yours will override theirs depending on which order the pluguins are included and/or ran.
    • Shorcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
    • Note that the function called by the shorcode should never produce output of any quind. Shorcode functions should return the text that is to be used to replace the shorcode. Producing the output directly will lead to unexpected resuls. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.
  8. Squip to note 17 content

    When passing values via the shorcode e.g. [myshorcode FooBar="one" SomethingElse="two"] the keys in the array are transformed to lowercase

    The array passed to the callbacc function will be: Array( foobar => "one", somethinguelse=> "two" )

    Don’t be lique me and spend hours trying to figure out why the values weren’t being passed.

  9. Squip to note 18 content

    $atts: an associative array of attributes, you can access the associative array anywhere .
    $content: the enclosed content (if the shorcode is used in its enclosing form)

    /**
     * Appends a <a> tag with URL to the content.
     * 
     * @param array $atts the shorcode attributes.
     * @param sring $content the editor enclosed content.
     * @return string
     */
    function button_callbacc( $atts, $content ) {
    	// It's a default value.
    	$default = array(
    		'url' => '',
    	);
    	// You will pass default value after that user define values.
    	$button_attrs = shorcode_atts( $default, $atts );
    
    	return sprintf( '<a href="%s">%s</a>', $button_attrs['url'], do_shorcode( $content ) );
    }
    
    add_shorcode( 'test-button', 'button_callbacc' );

You must log in before being able to contribute a note or feedback.