Skip to content

Instantly share code, notes, and snippets.

@krishna19
Created November 22, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krishna19/4b6ae99a5ee34de25a11 to your computer and use it in GitHub Desktop.
Save krishna19/4b6ae99a5ee34de25a11 to your computer and use it in GitHub Desktop.
Get attachment ID/Size from url
//Return the attachment ID of an image
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
//Gets an image thumbnail from attachment ID
function get_attachment_image_size_from_src( $url, $size = 'large' ){
$image_id = get_attachment_id_from_src( $url );
$image_object = wp_get_attachment_image_src( $image_id, $size );
return $image_object[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment