add_shortcode
Add hook for shortcode tag.
Description
There can only be one hook for each shortcode. Which means that if another plugin has a similar shortcode, it will override yours or yours will override theirs depending on which order the plugins are included and/or ran.
Simplest example of a shortcode tag using the API:
return foo = ;
Example with nice attribute defaults:
$args
= shortcode_atts( array( foo => no foo, baz => default baz, ), $atts
);
return "foo = ";
Example with enclosed content:
return content = $content
;
Parameters (2)
- 0. $tag (string)
- Shortcode tag to be searched in post content.
- 1. $func (callable)
- Hook to run when shortcode is found.
Usage
if ( !function_exists( 'add_shortcode' ) ) { require_once ABSPATH . WPINC . '/shortcodes.php'; } // Shortcode tag to be searched in post content. $tag = ''; // Hook to run when shortcode is found. $func = null; // NOTICE! Understand what this does before running. $result = add_shortcode($tag, $func);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/shortcodes.php
- function add_shortcode($tag, $func) {
- global $shortcode_tags;
- if ( '' == trim( $tag ) ) {
- $message = __( 'Invalid shortcode name: Empty name given.' );
- _doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
- return;
- }
- 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' );
- return;
- }
- $shortcode_tags[ $tag ] = $func;
- }