Created
October 6, 2017 19:31
-
-
Save danemorgan/b2f6beda40be1870f45126b9a3199364 to your computer and use it in GitHub Desktop.
Get the WordPress attachment ID of an image from it's URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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