Timthumb and Shortcodes for Easy WordPress Images

Here’s a quick and easy way to update image sizes on the fly directly in your WordPress posts. Add this code into your functions.php file in your theme. Then make sure you insert timthumb.php in the root of your theme directory. Next create a fully writable directory named ‘cache’ and place that in your theme root directory.

Now inside your posts just wrap an image URL with the img shortcode. Add height, width, alt, and class to it for more flexibility.

7/15/2011 – Updated to allow use within a text widget.

// Image resize in content
// To Use: {img w=600 h=200 class=alignleft alt=My Photo}http://mysite.com/photo.jpg{/img}
function timmyimg($atts, $content = null) {
	extract(shortcode_atts(array(
		'w' => '600',
		'h' => '250',
		'class' => 'alignnone',
		'alt' => ''
	), $atts));
	return "<img src='". get_bloginfo('template_directory') . "/timthumb.php?src=".$content."&w=".$w."&h=".$h."' alt='".$alt."' class='wp-post-img ".$class."' />";
}
add_shortcode('img', 'timmyimg');
 
// Allows this shortcode to work within a text widget
add_filter('widget_text', 'do_shortcode');

Forest

Forest

Forest

1 Comments
Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.