Posted By

distribuida on 09/14/11


Tagged

wordpress shortcode


Versions (?)

WordPress shortcode: Automatically insert image by file name


 / Published in: PHP
 

URL: http://www.wprecipes.com/wordpress-shortcode-automatically-insert-image-by-file-name

  1. //The first thing to do is to paste this code on your functions.php file:
  2.  
  3. function image_shortcode($atts, $content = null) {
  4. extract( shortcode_atts( array(
  5. 'name' => '',
  6. 'align' => 'right',
  7. 'ext' => 'png',
  8. 'path' => '/wp-content/uploads/',
  9. 'url' => ''
  10. ), $atts ) );
  11. $file=ABSPATH."$path$name.$ext";
  12. if (file_exists($file)) {
  13. $size=getimagesize($file);
  14. if ($size!==false) $size=$size[3];
  15. $output = "<img src='".get_option('siteurl')."$path$name.$ext' alt='$name' $size align='$align' class='align$align' />";
  16. if ($url) $output = "<a href='$url' title='$name'>".$output.'</a>';
  17. return $output;
  18. }
  19. else {
  20. trigger_error("'$path$name.$ext' image not found", E_USER_WARNING);
  21. return '';
  22. }
  23. }
  24. add_shortcode('image','image_shortcode');
  25.  
  26. /*
  27. Once done, you can use the image shortcode in your posts. For example, the following line of code: [image name=cat]
  28. */

Report this snippet  

You need to login to post a comment.