Languages: English • Italiano • Reference/has shortcode 日本語 (Add your language)
Checks whether the content passed contains a specific short code.
The short code needs to be registered with add_shortcode() to be recognized.
<?php if ( has_shortcode( $content, 'gallery' ) ) { } ?>
<?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.
}
?>
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');
has_shortcode() is located in wp-includes/shortcodes.php.