Skip to content

Instantly share code, notes, and snippets.

@danemorgan
Created October 6, 2017 19:31
Show Gist options
  • Save danemorgan/b2f6beda40be1870f45126b9a3199364 to your computer and use it in GitHub Desktop.
Save danemorgan/b2f6beda40be1870f45126b9a3199364 to your computer and use it in GitHub Desktop.
Get the WordPress attachment ID of an image from it's URL
/**
* Get the attachment ID of an image from it's URL.
*
* @link https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
*
* @param $image_url
*
* @return mixed
*/
function dmgist_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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment