Skip to content

Instantly share code, notes, and snippets.

@eduardozulian
Created September 30, 2013 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardozulian/6763772 to your computer and use it in GitHub Desktop.
Save eduardozulian/6763772 to your computer and use it in GitHub Desktop.
Retrieve the attachment ID from the file URL
<?php
/**
* Retrieve the attachment ID from the file URL
*
* @link http://pippinsplugins.com/retrieve-attachment-id-from-image-url/
*/
function horizonte_rural_get_attachment_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