Skip to content

Instantly share code, notes, and snippets.

@danemorgan
Last active June 30, 2021 08:59
Show Gist options
  • Save danemorgan/a8e6b5e505104e9541fec4d2ed91a229 to your computer and use it in GitHub Desktop.
Save danemorgan/a8e6b5e505104e9541fec4d2ed91a229 to your computer and use it in GitHub Desktop.
<?php
/**
* Get the attachment ID of an image from it's URL.
*
* @param string $image_url The URL for the image you need an id for.
*
* @return mixed
*/
function theme_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