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

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *