A blog for technology, SEO tips, website development and open source programming.

WordPress Link front-page image to their post url

0 61

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(
'<a1href="' . get_permalink($post->ID) . '"3><img4src="'.$upload_dir['baseurl'].'5"6>'
);
$text = preg_replace($patterns,$replacements,$text);
}
return $text;
}

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More