Snippet: Check current post for the existence of a short code

If you want to check if a short code exists before performing some action. `has_shortcode` is a custom function that will check short code’s availability.

Add following snippet to “functions.php”
Usage: has_shortcode(‘shortcode’);
Returns: true if shortcode exists otherwise false

Snippet

function has_shortcode( $shortcode = NULL ) {
    $post_to_check = get_post( get_the_ID() );
    $found = false;
    if ( ! $shortcode ) {
        return $found;
    }
    if ( stripos( $post_to_check->post_content, '[' . $shortcode) !== FALSE ) {
        $found = TRUE;
    }
    return $found;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>