WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/has shortcode

Description

Checks whether the content passed contains a specific short code.

The short code needs to be registered with add_shortcode() to be recognized.

Parameters

$content
(string) (required) The content to search.
Default: None
$tag
(string) (required) The short code to search for.
Default: None

Return

(boolean) 
True if the short code is found, false otherwise.

Usage

 <?php if ( has_shortcode$content'gallery' ) ) { } ?> 

Example

Simple Example

<?php
$content = 'This is some text, (perhaps pulled via $post->post_content). It has a [gallery] shortcode.';

if( has_shortcode( $content, 'gallery' ) ) {
	// The content has a [gallery] short code, so this check returned true.

}

?>

Simple Example

Enqueue some script when some post uses some shortcode.

function custom_shortcode_scripts() {
	global $post;
	if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) {
		wp_enqueue_script( 'custom-script');
	}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');

Change Log

Source File

has_shortcode() is located in wp-includes/shortcodes.php.

Related

Shortcode API

See also index of Function Reference and index of Template Tags.