add_shortcode

Add hook for shortcode tag.

Description

add_shortcode( (string) $tag, (callable) $func ); 

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

  1. if ( !function_exists( 'add_shortcode' ) ) { 
  2. require_once ABSPATH . WPINC . '/shortcodes.php'; 
  3.  
  4. // Shortcode tag to be searched in post content. 
  5. $tag = ''; 
  6.  
  7. // Hook to run when shortcode is found. 
  8. $func = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = add_shortcode($tag, $func); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/shortcodes.php  
  1. function add_shortcode($tag, $func) { 
  2. global $shortcode_tags; 
  3.  
  4. if ( '' == trim( $tag ) ) { 
  5. $message = __( 'Invalid shortcode name: Empty name given.' ); 
  6. _doing_it_wrong( __FUNCTION__, $message, '4.4.0' ); 
  7. return; 
  8.  
  9. if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) { 
  10. /** translators: 1: shortcode name, 2: space separated list of reserved characters */ 
  11. $message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' ); 
  12. _doing_it_wrong( __FUNCTION__, $message, '4.4.0' ); 
  13. return; 
  14.  
  15. $shortcode_tags[ $tag ] = $func;