Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

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.

share|improve this answer

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://your-default-img-path.com/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");
share|improve this answer
    
I'd like the user to be able to have buttons that don't have images as well, though. This would still require me to check if the user had entered an image. –  Rev May 1 at 19:29
    
@Rev Well then the user would enter the parameter that is empty - this would overwrite the default url strong with empty path thus creating a button without image. –  Borek May 1 at 19:31
    
This gave me an idea of how to go about it. I've posted my solution as an answer. I gave you an up-vote as a thanks :) –  Rev May 1 at 19:40
    
@Rev I've jsut got what you mean after the comment and come up with my own solution but as I can see you've got it already. –  Borek May 1 at 19:42
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.