This function returns the wordpress attachment ID of a given image src. Useful if you need to get a different size version of the same image. Snippet from jameslafferty @ http://wordpress.org/support/topic/need-to-get-attachment-id-by-image-url
UPDATED: to include refinements, see comments below. This will work with image urls that have been resized by WordPress also.
function get_attachment_id_from_src ($link) {
global $wpdb;
$link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");
}
//Get a different size image
$attachment_id = get_attachment_id_from_src( $src );
$medium_im = wp_get_attachment_image_src( $attachment_id, 'medium');
//wp_get_attachment_image_src() is a wordpress function that returns an array with {src, width,height} info.
echo '<img src="'.$medium_im[0].'" alt="" width="'.$medium_im[1].'" height="'.$medium_im[2].'" />';
How do you get this to work if the src you are working with has size details in it like -300×200.jpg
WordPress stores the additional image sizes as a serialised string in the postmeta table. So to use an image src with dimensions we’d need to change the sql. Try this, it isn’t thoroughly tested but it worked with the image I tested on.
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); if($id == null){ $image_src = basename ( $image_src ); $q2 = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%$image_src%'"; $id = $wpdb->get_var($q2); } return $id; }The other way you could tackle it is use regex to remove the ‘-100×100′ part of the src string. Although I think it’s safer to modify the sql.
Actually, thinking about that, if you had an image with the same name in different month folders this could cause a problem with possibly the wrong result being returned. Because it’s serialised data its a bit difficult to work with. You could possibly change to grab all possible values and reserialise the data to work with the results in php but this could be a bit messy. Maybe stripping the dimensions from the original string could be safer…
Going down the regex path to allow for resized images: try this.
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; }That’s a nice solution, but I’d like to improve it just a bit. There’s no need to remove the extension and then add it back in.
Here’s what I settled for:
function get_image_id_by_link($link)
{
global $wpdb;
$link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");
}
P.S. why did you put that question mark in the regex? Is it possible to have a size suffix without the second dimension?
Cheers, that makes for a nicer solution…