Created
May 6, 2011 08:50
-
-
Save fumikito/958651 to your computer and use it in GitHub Desktop.
WordPressで画像のURLからアタッチメントIDを取得する
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
<?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