6

I'm using do_shortcode function to add shortcode in my template. But i would like to check if that shortcode exists before display them.

I mean like this

If (shortcode_gallery_exists) {
  echo do_shortcode('[gallery]');
}

Can anyone help me? Thanks

7 Answers 7

8

#23572 introduced shortcode_exists() into 3.6.

7

I think you could use the following code to do this:

$content = get_the_content();
//write the begining of the shortcode
$shortcode = '[gallery';

$check = strpos($content,$shortcode);
if($check=== false) {
  //Code to execute if there isn't the shortcode
} else {
  //Code to execute if the shortcode is present
}

(Caveat: not tested)

1
  • This works, without the "[" in the shortcode name. Commented Jan 18, 2023 at 10:18
3

Found this online somewhere and used is once or twice

//first we check for shortcode in the content
$tempContent = get_the_content();
$tempCheck = '[gallery';

$tempVerify = strpos($tempContent,$tempCheck);
if($tempVerify === false) {
  //Your Shortcode not found do nothing ? you choose
} else {
    echo do_shortcode('[gallery]');
}

.

(i know the [gallery is missing the ].. leave it like so)

This should be used inside the loop ..
Hope this helps, Sagive

1
  • 2
    OMG @Ryan beat me to it.. :) we posted a few seconds apart i guess cuz i didnt see hes answer :) apologies ryan - you came first..
    – Sagive
    Commented Mar 15, 2012 at 17:27
3

You can create your own function,

// check the current post for the existence of a short code
function has_shortcode( $shortcode = NULL ) {

    $post_to_check = get_post( get_the_ID() );

    // false because we have to search through the post content first
    $found = false;

    // if no short code was provided, return false
    if ( ! $shortcode ) {
        return $found;
    }
    // check the post content for the short code
    if ( stripos( $post_to_check->post_content, '[' . $shortcode) !== FALSE ) {
        // we have found the short code
        $found = TRUE;
    }

    // return our final results
    return $found;
}

The in your template write a conditional like,

if(has_shortcode('[gallery]')) {  
    // perform actions here  
} 

Idea from this NetTuts link

1
1

WP now has a built-in function for this:

<?php
 
if ( shortcode_exists( 'gallery' ) ): ?>

    // The short code exists.

<?php else: ?>

   // It DOESN'T EXIST

<?php endif;?>

https://developer.wordpress.org/reference/functions/shortcode_exists/

0

WordPress allows you to check whether a shortcode exists or not.

To check it you can use shortcode_exists() function. It returns true if the shortcode exists.

<?php if ( shortcode_exists( $tag ) ) { } ?>

Where $tag is the name of shortcode you want to check.

<?php
if ( shortcode_exists( 'latest_post' ) ) {
    // The short code exists.
}
?>
0

I know it's old question but someone may still need this, especially because shortcode_exists() may not work for some plugin shortcodes:

$shortcode_result = do_shortcode('[your_shortcode]');

if ($shortcode_result != [your_shortcode]) && 
    !empty($shortcode_result))] {

    // do what you need

} else {
    // this is not valide shortcode
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.