Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

has_shortcode

Function Description:

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Check if a post content has a specific shortcode
$post_content = get_the_content();
if ( has_shortcode( $post_content, 'gallery' ) ) {
    echo 'This post contains the gallery shortcode.';
} else {
    echo 'This post does not contain the gallery shortcode.';
}
// Example 2: Check if a specific shortcode is present in a custom field
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_name', true );
if ( has_shortcode( $custom_field_value, 'video' ) ) {
    echo 'This custom field contains the video shortcode.';
} else {
    echo 'This custom field does not contain the video shortcode.';
}
// Example 3: Avoid common pitfall - using has_shortcode within the loop
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $post_content = get_the_content();
        if ( has_shortcode( $post_content, 'audio' ) ) {
            echo 'This post contains the audio shortcode.';
        } else {
            echo 'This post does not contain the audio shortcode.';
        }
    }
}