WordPress: Get attachment_id from source

Wooop
[crayon lang=”php”]

public static function getAttachmentIdFromSource($source) {
global $wpdb;
$source= preg_replace(‘/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i’, ”, $source);
return $wpdb->get_var(“SELECT ID FROM {$wpdb->posts} WHERE guid=’$source'”);
}

[/crayon]

Leave a Reply

Your email address will not be published. Required fields are marked *