Especially when developing plugins or theme settings that make use of the WordPress media uploader to upload and insert images, being able to retrieve the ID of the image uploaded can be extremely useful. By grabbing the image (attachment) ID, you suddenly have a lot more control over what you can do with the image, such as display one of the different sizes that WordPress generates when the image is uploaded.
This is a little function I wrote while on the train home from Chicago.
// retrieves the attachment ID from the file URL function pippin_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); return $attachment[0]; } |
Simply pass the URL of the image ID you wish to retrieve. Once you’ve done that, you could do the following to retrieve the auto-generated thumbnail size of the image:
// set the image url $image_url = 'http://yoursite.com/wp-content/uploads/2011/02/14/image_name.jpg'; // store the image ID in a var $image_id = pippin_get_image_id($image_url); // retrieve the thumbnail size of our image $image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail'); // display the image echo $image_thumb[0]; |
Enjoy!
Does it work with image URLs that are not the main image? Resized versions, etc.
No, you will need the original URL, but you can then use the ID to get the other sizes.
Very useful. Great work!
Hey Pippin,
This is a rad little snippet. I rewrote it slightly to directly retrieve the desired thumb size as it can become frustrating to continually rewrite the last 3 steps.
Excellent!
I tried this out, but it seems that the “guid” is not in all cases the URL to the picture.
That is true, the guid column will not always work. There was a tutorial someone posted not too long ago with a better way to do this, but I don’t remember which site it was on unfortunately.
After combining some code from several other people who have posted about this online, here’s a function I wrote that doesn’t use GUIDs, and even works with image thumbnail URLs:
Pingback: Get the Attachment ID from an Image URL in WordPress - Philip Newcomer
Looks like the comment form
tag doesn't format the text nicely. Here's my blog post with the code formatted for easier reading: http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/
Looks great! FYI, you can post code by wrapping it in PRE tags.
I see. The PRE tag wasn’t in the list of allowed tags below the comment box, so I used CODE. ๐
I am using this and just upgraded to WP 3.5. Now I get this error: Warning: Missing argument 2 for wpdb::prepare(),. I found the post about how it should work now (http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/) but am having trouble changing this code so that it is correct and not throwing the error. Can anyone help me with this?
Thanks!
I’ve updated the code to fix the error.
That works again. Thank you so much for your help!
ty for update!
Take it back, nothing has changed? Same 3.5 update problem
okay, jk it works. maybe put your example function in one block so it doesn’t look so janky? Thanks for this btw.
Whoops, fixed!
Hi Pippin,
Great tip. Using this to get the attachment ID of a logo uploaded via the WP customizer.
One quick question: Is there a reason why you use
$wpdb->prefix
instead of just using$wpdb->posts
inside the SQL? Since it’s a default table should$wpdb->posts
always have the correct prefix anyway?Cheers,
Eric
Nope, no reason ๐
How do I use this to display the thumbnail of a custom image size I created using add_image_size? For example I am trying to display the thumbnail size for the ‘home-page-slide’ size that was created using the code below.
This code works great to display the thumbnail of a full size image but if I select the Home Page Slide size there is not a thumbnail that displays. (I am trying to display these thumbnails in the meta box I have created in an image slider).
//Add Post Thumbnail Support
if (function_exists('add_theme_support')) { // as of WP 2.9
add_theme_support( 'post-thumbnails', array( 'newsletter','homeslider' ) );//activate thumbnails for these custom post types
add_image_size('newsletterlg', 300, 200, true);//image size for display in post
add_image_size('newsletterthumb', 132, 107, true);//thumbnail for main newsletter page
add_image_size( 'home-page-slide', 1200, 355, true );
add_image_size( 'product-slide', 203, 131, true );
}
//add custom image sizes to Sizes selection in media uploader
function custom_in_post_images( $args ) {
$custom_images = array('home-page-slide' => 'Home Page Slide', 'product-slide' => 'Product Slide'); return array_merge( $args, $custom_images );
}
add_filter( 'image_size_names_choose', 'custom_in_post_images' );
Quick spot. Current code has HTML number instead of “->” which causes a straight copy/paste to fail
Hi Pippin,
great tip indeed!
Just wanna try a little further… does it only work with images or even with other attachment type (such as pdf i.e.)?
thanks
and again, great tip!
Bye
Carlo
Yep!
Looks like your “greater than” sign got replaced by html name on fourth line of first code snippet.
Create little snippet! I’ve modified it slightly for my needs.
function url_to_attachmentid( $image_url ) {
if ( empty( $image_url ) )
return null;
global $wpdb;
$attachment = wp_cache_get( ‘featured_column_thumbnail_’ . md5( $image_url ), null );
if ( false === $attachment ) {
$attachment = $wpdb->get_col( $wpdb->prepare( “SELECT ID FROM $wpdb->posts WHERE guid = ‘%s’;”, $image_url ) );
wp_cache_add( ‘featured_column_thumbnail_’ . md5( $image_url ), $attachment, null );
}
return !empty( $attachment ) ? $attachment[0] : null;
}
Nice ๐
Thanks for the snippet Pippin!
I’ve used this many times, always without issue. Today, I moved a site from one server to another (Bluehost to GoDaddy), and now it just returns empty src=”” for all images. Its still working fine on the copy of the site on Bluehost, but the copy on GoDaddy gives me
Any thought son this? Totally confused.
Pingback: Get Attachment ID from a WordPress Image URL - WP Scholar
Hello,
I would be really really happy to have an anwser, even if it’s impossible ๐
I had a developper who export an old php blog (it wasn’t a cms) and build a new wordpress really great. He left me during the process : I have to export this blog into 2 MU sites. I already did that, it’s not a problem. But! Big but !
He didn’t worked with the good practice on librairy. He worked with ID (that what he said).
So I have my posts with good thumbnail, even if he put aaaaall images (8000) in a same folder (/upload without under folder with dates).
When I want to export a part of this blog in xml I think wp can’t find images so nothing in xml code on url images, and impossible to import…
Do you see my problem and is it possible to make something…?
The posts have a featured image, but I don’t know where it’s declared, because xml export everything but images.
I have to find a way to tell wp to export real image it has in thumbnail/featured image (and not the way it used to, in folder/yyyy/mm/dd) OR something to rebuilt the librairy and put images in good folders…
Thank you a lot for your answer.
I have 1000 posts to repost in 2 differents domains, and it could be really long to reimport featured images with my hands :/
simple and perfect, good work (y)
Thanks for finally writing about >Retrieve Attachmewnt
ID from Imagte URL | Pippins Plugins <Loved it!
Since version 4.0, you can use attachment_url_to_postid( $url ).
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
Use this native WP function instead
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
Works for sure.
Hi everyone. I’ve got much simpler way to get another size of image without id and without sql and this kind of stuff.
Just use:
$new_image_url = str_replace(“.jpg”, “-150×150.jpg”, $image_url);
In uploads you can check wich sizes of images do you have, and thats all ๐
Hope helped anyone ๐
Yep! Worked – I like you made the function name with word “pippin”. Nice way to branding
AMAAAAZIIIIIIIIIIIIIIIIIINGGGGGGGGG YOU RULE
Every time I found your article on google, I feels I ‘ll get my answer. really amazing efforts you put on your article to make it easier to understand.
Every time I found your article on google, I feels I โll get my answer. really amazing efforts you put on your article to make it easier to understand.
Pingback: ็ปๅใฎURLใใใใใฎ็ปๅใฎIDใๅๅพใใ | ใฌใธใงใใใฌใใฅใผ้ๅ ฑ๏ผ
You can get this more faster using a MySQL index on guid,post_type columns, I suppose.