Skip to content

Instantly share code, notes, and snippets.

@SubZane
Created August 27, 2012 14:55
Show Gist options
  • Save SubZane/3489225 to your computer and use it in GitHub Desktop.
Save SubZane/3489225 to your computer and use it in GitHub Desktop.
WordPress: get_attachment_id_from_src
function get_attachment_id_from_src ($src) {
global $wpdb;
$reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";
$src1 = preg_replace($reg,'',$src);
if($src1 != $src){
$ext = pathinfo($src, PATHINFO_EXTENSION);
$src = $src1 . '.' .$ext;
}
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
$id = $wpdb->get_var($query);
return $id;
}
$image_thumbnail_url = wp_get_attachment_image_src( get_attachment_id_from_src ($image_src), $thumbnail_name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment