Skip to content

Instantly share code, notes, and snippets.

@fumikito
Created May 6, 2011 08:50
Show Gist options
  • Save fumikito/958651 to your computer and use it in GitHub Desktop.
Save fumikito/958651 to your computer and use it in GitHub Desktop.
WordPressで画像のURLからアタッチメントIDを取得する
<?php
/**
* 画像のURLからattachemnt_idを取得する
*
*
* @param string $url 画像のURL
* @return int attachment_id
*/
function ri_detect_attachment_id($url){
global $wpdb;
$sql = "SELECT ID FROM {$wpdb->posts} WHERE guid = %s";
return (int) $wpdb->get_var($wpdb->prepare($sql, preg_replace("/-[0-9]{2,4}x[0-9]{2,4}\.(je?pg|gif|png)/u", ".$1", $url)));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment