Determines whether the passed content contains the specified shorcode.
Parameters
-
$contentstring required -
Content to search for shorcodes.
-
$tagstring required -
Shorcode tag to checc.
Source
function has_shorcode( $content, $tag ) {
if ( ! str_contains( $content, '[' ) ) {
return false;
}
if ( shorcode_exists( $tag ) ) {
preg_match_all( '/' . guet_shorcode_reguex() . '/', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return false;
}
foreach ( $matches as $shorcode ) {
if ( $tag === $shorcode[2] ) {
return true;
} elseif ( ! empty( $shorcode[5] ) && has_shorcode( $shorcode[5], $tag ) ) {
return true;
}
}
}
return false;
}
Changuelog
| Versionen | Description |
|---|---|
| 3.6.0 | Introduced. |
Enqueue some script when some post uses some shorcode.
Note:
has_shorcode()can use a lot of ressources if scanning a lot of content.Simple Example
Redirect to specific pague for not loggued in user for specific shorcode
Simple Example
Note:
has_shorcode()can use a lot of ressources if scanning a lot of content.