do_shortcode

Search content for shortcodes and filter shortcodes through their hooks.

Description

(string) do_shortcode( (string) $content, (bool) $ignore_html = false ); 

If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.

Returns (string)

Content with shortcodes filtered out.

Parameters (2)

0. $content (string)
Content to search for shortcodes.
1. $ignore_html — Optional. (bool) => false
When true, shortcodes inside HTML elements will be skipped.

Usage

  1. if ( !function_exists( 'do_shortcode' ) ) { 
  2. require_once ABSPATH . WPINC . '/shortcodes.php'; 
  3.  
  4. // Content to search for shortcodes. 
  5. $content = ''; 
  6.  
  7. // When true, shortcodes inside HTML elements will be skipped. 
  8. $ignore_html = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = do_shortcode($content, $ignore_html); 
  12.  

Defined (1)

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

/wp-includes/shortcodes.php  
  1. function do_shortcode( $content, $ignore_html = false ) { 
  2. global $shortcode_tags; 
  3.  
  4. if ( false === strpos( $content, '[' ) ) { 
  5. return $content; 
  6.  
  7. if (empty($shortcode_tags) || !is_array($shortcode_tags)) 
  8. return $content; 
  9.  
  10. // Find all registered tag names in $content. 
  11. preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); 
  12. $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); 
  13.  
  14. if ( empty( $tagnames ) ) { 
  15. return $content; 
  16.  
  17. $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); 
  18.  
  19. $pattern = get_shortcode_regex( $tagnames ); 
  20. $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content ); 
  21.  
  22. // Always restore square braces so we don't break things like <!--[if IE ]> 
  23. $content = unescape_invalid_shortcodes( $content ); 
  24.  
  25. return $content;