Q:

Checking if an attribute exists in a shortcode

I've written a small plugin that spits out html for a button. It works great, except for one thing: If the user doesn't specify an image, the image appears broken. Is there a way for me to check and see if the user has specified an image, and only print the image code if that's the case?
Here's my plugin code:
function button_shortcode($args) {
    return "<a class=\"button\" href=\"" . $args["url"] . "\"><img alt=\"" . $args["bigtext"] . "\" class=\"alignleft\" src=\"" . $args["img"] . "\" /><small>" . $args["smalltext"] . "</small>" . $args["bigtext"] . "</a>";
}
add_shortcode("button", "button_shortcode");

Here's the shortcode:
[button url="http://www.example.com/" img="/path/to/image.png" smalltext="Smaller Text" bigtext="Bigger Text"]

A:

Okay, I've re-written the function so that it does the following:

checks that your array values are set before using them. You can't always be sure they'll be there.
if the image is empty, it returns with an error message. Not sure if this is what you want, but you can just remove it.
first creates your image html.
then, if there's a URL specified, it wraps up the image in your link HTML.  If not, it'll just return your image HTML.

Note the way I've created your HTML.  Breaking and opening strings and escaping quotes etc. is messy and difficult to read.  Use single quotes when constructing HTML strings so you can use double-quotes without escaping them.
I've also used sprintf() and then substitute each piece I want.  Much easier to read, and modify later.
Hope it helps!
function button_shortcode($args) {

    if ( empty( $args['img'] ) ) {
        return 'Error - no image source was specified';
    }

    $sBigText = isset( $args['bigtext'] )? $args['bigtext'] : '';
    $sSmallText = isset( $args['smalltext'] )? $args['smalltext'] : '';

    $sHtmlToReturn = sprintf( '<img alt="%s" class="alignleft" src="%s" /><small>%s</small>%s', $sBigText, $args['img'], $sSmallText, $sBigText );

    if ( !empty( $args['url'] ) ) {
        $sHtmlToReturn = sprintf( '<a class="button" href="%s">%s</a>', $args['url'], $sHtmlToReturn );
    }

    return $sHtmlToReturn;
}

Paul.

A:

Borek pointed in the right direction, here's my fine code*:
function button_shortcode($attributes, $content) {
    if ($attributes["image"] != "") {
        $img = "<img alt=\"" . $content . "\" class=\"alignleft\" src=\"" . $attributes["image"] . "\" />";
    } else {
        $img = "";
    }
    if ($attributes["intro"] != "") {
        $intro = "<small>" . $attributes["intro"] . "</small>";
    }
    return "<a class=\"button\" href=\"" . $attributes["link"] . "\">" . $img . $intro . $content . "</a>";
}
add_shortcode("button", "button_shortcode");

*Note: I did change some of the attribute names and stuff as well, but that wasn't necessary. 

