has_shortcode

Whether the passed content contains the specified shortcode.

Description

has_shortcode( (string) $content, (string) $tag ); 

Parameters (2)

0. $content (string)
Content to search for shortcodes.
1. $tag (string)
Shortcode tag to check.

Usage

  1. if ( !function_exists( 'has_shortcode' ) ) { 
  2. require_once ABSPATH . WPINC . '/shortcodes.php'; 
  3.  
  4. // Content to search for shortcodes. 
  5. $content = ''; 
  6.  
  7. // Shortcode tag to check. 
  8. $tag = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = has_shortcode($content, $tag); 
  12.  

Defined (1)

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

/wp-includes/shortcodes.php  
  1. function has_shortcode( $content, $tag ) { 
  2. if ( false === strpos( $content, '[' ) ) { 
  3. return false; 
  4.  
  5. if ( shortcode_exists( $tag ) ) { 
  6. preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER ); 
  7. if ( empty( $matches ) ) 
  8. return false; 
  9.  
  10. foreach ( $matches as $shortcode ) { 
  11. if ( $tag === $shortcode[2] ) { 
  12. return true; 
  13. } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) { 
  14. return true; 
  15. return false;