Search code examples
phpwordpressshortcode

How to check to see if wordpress shortcode has $content


I have a shortcode that is self enclosing which i will use a post ID to pull content from but when I'm not using the self enclosing part of the shortcode and just the shortcode I want it to use the current posts ID so how do I check to see if there is any content inside the self enclosing?

When I want to pull the content from another post I will us the shortcode like so [deck]172[/deck] which 172 would be the post ID that I'm going to pull the content from but if I want to pull the content from the current post I would like to use just [deck] so I'm not sure how I can check to see if there is anything in the self enclosing

add_shortcode( 'deck', 'deck_shortcode' );
function deck_shortcode( $atts ,$content = null) {

    if (Check to see if $content exist)  {
       $deck_id = $content;
    } else {
        $deck_id = $post->ID;
    }

}

This is the type of logic i'm looking for I'm just not sure how to check to see if the shortcode is self enclosing and would have anything in the $content


Solution

  • empty() is what you're looking for.

    if (!empty($content))  { // if not empty, then
           $deck_id = $content; // do this
        } else {
            $deck_id = $post->ID;
        }