On this page
has_shortcode( string $content, string $tag ): bool
Determines whether the passed content contains the specified shortcode.
Parameters
$contentstring Required-
Content to search for shortcodes.
$tagstring Required-
Shortcode tag to check.
Return
bool Whether the passed content contains the given shortcode.
Source
File: wp-includes/shortcodes.php. View all references
function has_shortcode( $content, $tag ) {
if ( false === strpos( $content, '[' ) ) {
return false;
}
if ( shortcode_exists( $tag ) ) {
preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return false;
}
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] ) {
return true;
} elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
return true;
}
}
}
return false;
}
Related
Uses
| Uses | Description |
|---|---|
| shortcode_exists() wp-includes/shortcodes.php | Determines whether a registered shortcode exists named $tag. |
| get_shortcode_regex() wp-includes/shortcodes.php | Retrieves the shortcode regular expression for searching. |
| has_shortcode() wp-includes/shortcodes.php | Determines whether the passed content contains the specified shortcode. |
Used By
| Used By | Description |
|---|---|
| wp_ajax_parse_embed() wp-admin/includes/ajax-actions.php | Apply [embed] Ajax handlers to a string. |
| has_shortcode() wp-includes/shortcodes.php | Determines whether the passed content contains the specified shortcode. |
| get_post_galleries() wp-includes/media.php | Retrieves galleries from the passed post’s content. |
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/has_shortcode