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"]

Solutions Collecting From Web of "Checking if an attribute exists in a shortcode"

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.

How about just assign a default image to this variable? So if no image is specified a default (instead of empty) image url is used? BTW. What’s with those backslashes? Just use single and double quotes.

function button_shortcode($args) {
    extract(shortcode_atts($default=array(
        'url' => 'http://default.com',
        'bigtext' => 'Your default big text',
        'img' => 'http://img.mywordpresstips.com/plugins/img.png',
        'small' => 'Your default small text',
    ), $args));
    return '<a class="button" href="' . $url . '"><img alt="' . $bigtext . '" class="alignleft" src="' . $img . '" /><small>' . $smalltext . '</small>' . $bigtext . '</a>';
}
add_shortcode("button", "button_shortcode");

Now if you need to output separate buttons (if image is present or not) then use conditionals like so:

function button_shortcode($args) {
    extract(shortcode_atts($default=array(
        'url' => 'http://default.com',
        'bigtext' => 'Your default big text',
        'img' => '',
        'small' => 'Your default small text',
    ), $args));

    if (!empty($url)) {
        $button_output = '<a class="button" href="' . $url . '"><img alt="' . $bigtext . '" class="alignleft" src="' . $img . '" /><small>' . $smalltext . '</small>' . $bigtext . '</a>';
    } else { 
        $button_output = '<a class="button" href="' . $url . '"><small>' . $smalltext . '</small>' . $bigtext . '</a>';
    }

    return $button_output;
}
add_shortcode("button", "button_shortcode");

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.