SEO friendly timthumb on wordpress
Many of you love using timthumb in your wordpress theme but don’t use it because it does not generates seo friendly urls for images. I was asked by a client to make timthumb urls seo friendly and here is how I did that.
First open your wordpress site .htaccess file. Generally it is located in root of your wordpress installation.
It should look like this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Now create new folder in your root call it let’s say images and place cache folder and timthumb in there then edit your .htaccess file as below
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(images) [NC]
RewriteRule . /index.php [L]
RewriteRule ^images/(.*)x(.*)/r/(.*) images/timthumb.php?src=http://example.com/$3&w=$1&h=$2&zc=1&q=100&a=tl
# END WordPress
Replace example.com with you site name:
Now upload htaccess file and where you need to use timthumb do as below
<?php
if (has_post_thumbnail()):
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
echo '<a href="'.get_permalink().'"><img alt="'.get_the_title().'" src="'.get_bloginfo('url').'/images/715x440/r/'.str_replace(array('http://example.com/', 'http://www.example.com/'), array('',''), $thumbnail[0]).'" width="715" height="440" /></a>';
endif;
?>
That should do the trick for making timthumb url seo friendly.
Nice helped me. thank you
Nice trick..
Is there a way to use this by getting the first image in the post instead of the attachment.
if you mean image inserted in the post body, then you will have to first get the image by using regexp from the post body. then you can use that image is place of source for image. need to make sure image is from allowed domain name in your timthumb script.
Perfect! The query string in timthumb has been bugging me. I hate that the img src URL has been a php file rather then a image file.
Can’t wait to try this out later!
Thanks so much for sharing
The last part of your code does not seem to work. It will show the raw code instead of the image.
hello,
I have updated the code.
Hi,
what if my images are in year/month folder? do you have any solution for that?
this should work for images on all folders where timthumb can access.