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 have a shortcode that display an image when you use the image url inside it.

function image_code($atts, $content = null) {
    return '<img src="'.$content.'" class="user-imgs"/>';
}
add_shortcode('img', 'image_code');

So when I use the code like this:

[img]https://www.google.com.br/images/srpr/logo3w.png[/img]

it display only the image like this:(in the css I have set the .user-imgs to max-width 600px)

<img src="https://www.google.com.br/images/srpr/logo3w.png">

But I want to display a link to the full image, like this:

<a href="https://www.google.com.br/images/srpr/logo3w.png"><img src="https://www.google.com.br/images/srpr/logo3w.png">

How I can retrieve the image url used in shortcode and use it as a link in to the original image?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can reuse the $content parameter:

function image_code($atts, $content = null) {
    $url = esc_url( $content );
    return "<a href='$url'><img src='$url' class='user-imgs' /></a>";
}

Or pass the URL as parameter in case you want to use a different URL:

function image_code($atts, $content = null) {

    $args = shortcode_atts( array( 'url' => FALSE ), $atts  );
    $img  = esc_url( $content );
    $url  = $args['url'] ? esc_url( $args['url'] ) : $img;

    return "<a href='$url'><img src='$img' class='user-imgs' /></a>";
}
share|improve this answer
    
Thank you so much!!! I used the first code, and it worked. Now it returns the correct url! –  RogerNobrega Jan 5 '13 at 7:48
add comment

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.