Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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