Skip to content

Instantly share code, notes, and snippets.

@CarlLee
Last active December 16, 2015 10:08
Show Gist options
  • Save CarlLee/5417857 to your computer and use it in GitHub Desktop.
Save CarlLee/5417857 to your computer and use it in GitHub Desktop.
function embed_images($content){
$embed_images = preg_replace_callback('/https?:\/\/([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?/i', "addimg",$content);
return $embed_images;
}
function addimg($url){
global $post;
$title = $post ->post_title;
$imgurl = preg_replace('/((^http[s]{0,1}:)\/\/(.+).(gif|jpg|bmp|png))/i', '<img src="$1" alt="'.$title.'" /><pre>$1</pre>',$url[0]);
if($imgurl !== $url[0]){
// return the replace output if it's an image
return $imgurl;
}else{
// return the orinal matched url if it's not an image
return $url[0];
}
}
add_filter('the_content', 'embed_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment