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 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'); 
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  Fernando Baltazar May 10 '13 at 16:39
    
Oh yeah, I didn't see that. Glad it's all working :) –  George Pearce May 10 '13 at 17:11

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.