Shortcodes: Return different based on atts

All you need is some very simple IF logic.

The $author variable is set to be empty here "author" => '' unless an author name is used in your shortcode.

So now that you know the default value of $author you can use IF logic to check if it is empty or not. Two logic operators that are useful are == (equal to) and != (not equal to). We can use either operator but we only need one.

So first let’s check if $author is empty.

if ( $author == '' ) {

    //do something here

}

But now we need to do something if $author is not empty. So we can add else to our logic.

if ( $author == '' ) {

    //do something here if $author is empty

} else {

    //do something here if $author is not empty

}

The part of your shortcode function that send the result to the page is this.

return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

So now we can put that inside our IF logic.

if ( $author == '' ) {

    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

} else {


    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

}

But right now the shortcode function returns the same results to the page. We need to adjust the first return line for when there is no author. That looks like this.

if ( $author == '' ) {

    return '<blockquote>'.$content.'</blockquote>';

} else {


    return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';

}

And the final shortcode.

//Pullquote shortcode
function pullQuotes($atts, $content = null) {
    extract(shortcode_atts(array(
        "author" => ''
    ), $atts));
    if ( $author == '' ) {
        return '<blockquote>'.$content.'</blockquote>';
    } else {
        return '<blockquote>'.$content.'<p class="quote-author">' . $author . '</p>    </blockquote>';
    }
}
add_shortcode("pullquote", "pullQuotes");