3

I have the following code for showing images using the shortcode image but the shortcode is [image name=logo] to load an image from /uploads/ directory called logo.png; changing the name I can load different images just like dog.png bullet.png etc. writting after name= the name file.

So my question is how to make the function to write on shortcode [logo] and I can get the image logo.png

function image_shortcode($atts, $content = null) {
extract( shortcode_atts( array(
    'name' => '',
    'align' => 'right',
    'ext' => 'png',
    'path' => '/wp-content/uploads/',
    'url' => ''
    ), $atts ) );
    $file=ABSPATH."$path$name.$ext";
    if (file_exists($file)) {
        $size=getimagesize($file);
        if ($size!==false) $size=$size[3];
        $output = "<img src='".get_option('siteurl')."$path$name.$ext' alt='$name' $size align='$align' class='align$align' />";
        if ($url) $output = "<a href='$url' title='$name'>".$output.'</a>';
        return $output;
    }
    else {
        trigger_error("'$path$name.$ext' image not found", E_USER_WARNING);
        return '';
    }
}
add_shortcode('image','image_shortcode'); 

1 Answer 1

5

Change

'name' => '',

to

'name' => 'logo',

Then when there is no name field passed to the shortcode, it will default to logo, which will load logo.png.

2
  • Thanks! all it worked perfectly, also I change the line add_shortcode('image','image_shortcode'); to: add_shortcode('logo','image_shortcode'); just for user friendly usage :) now I type [logo] and logo image appears. May 10, 2013 at 16:39
  • Oh yeah, I didn't see that. Glad it's all working :) May 10, 2013 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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