wp-snippet

This post simply shows how you can link the front-page wordpress images to their post url.

You need to copy & paste the following code inside theme functions.php

add_filter('the_content', 'link_images_to_post');
function link_images_to_post($text){
$upload_dir = wp_upload_dir();
$upload_url = preg_quote($upload_dir['baseurl'], '/');
global $post;
if(empty($post->ID))
return $text;
// here starts the regex craziness!!
if(preg_match('/<a(.*?)href="'.$upload_url.'(.*?)"(.*?)><img(.*?)src="'.$upload_url.'(.*?)"(.*?)><\/a>/', $text)){
//there is a linked image attachement in the post_content
$patterns = array(
'/<a(.*?)href="'.$upload_url.'(.*?)"(.*?)><img(.*?)src="'.$upload_url.'(.*?)"(.*?)><\/a>/'
);
$replacements = array(
'<a\1href="' . get_permalink($post->ID) . '"\3><img\4src="'.$upload_dir['baseurl'].'\5"\6>'
);
$text = preg_replace($patterns,$replacements,$text);
}
return $text;
}