Skip to content

Instantly share code, notes, and snippets.

@annalinneajohansson
Last active December 20, 2015 09:59
Show Gist options
  • Save annalinneajohansson/6112600 to your computer and use it in GitHub Desktop.
Save annalinneajohansson/6112600 to your computer and use it in GitHub Desktop.
Detect if a specific shortcode is being used in post_content. Returns a boolean.
<?php
function hip_detect_shortcode( $shortcode = false ) {
$shortcode_found = false;
if( $shortcode && is_singular() ) {
global $post;
$pattern = get_shortcode_regex();
if( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( $shortcode, $matches[2] ) ) {
$shortcode_found = true;
}
}
return $shortcode_found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment