attachment_url_to_postid()
Trying to get the attachment ID (image from the media library) by the specified attachment URL.
To get the post/page ID instead of the attachment, use the similar function url_to_postid().
Hooks from the function
Returns
Int. Post ID (attachment) or 0 if the attached file could not be found.
Usage
$attachment_id = attachment_url_to_postid( $url );
- $url(string) (required)
Attachment URL from which to get the ID.
You need to pass the URL of the original image. The URL of the thumbnail will not work. That is, http://example.com/wp-content/uploads/2016/11/pic.jpg will work, but http://example.com/wp-content/uploads/2016/11/pic-300x189.jpg will not.
Examples
#1 Demo
The code below shows all the options and what the function will return. The last example shows how to get the ID of an attachment if a thumbnail link is passed: there the thumbnail size is cut out by regular expression.
$url = 'http://example.com/id_6643/proverka.html/prvoerka-vhodyashhih-dannyh';
$attachment_id = attachment_url_to_postid( $url ); // 0
$url = 'http://example.com/wp-content/uploads/2016/01/Prvoerka.jpg';
$attachment_id = attachment_url_to_postid( $url ); // 7704
$url = 'http://example.com/wp-content/uploads/2016/01/Prvoerka-80x80.jpg';
$attachment_id = attachment_url_to_postid( $url ); // 0
// remove the thumbnail size (-80x80) from the URL
$url = preg_replace( '~-[0-9]+x[0-9]+(?=\..{2,6})~', '', $url );
$attachment_id = attachment_url_to_postid( $url ); // 7704
#2 Custom function to get attachment ID via URL
/**
* Gets attachment ID by URL.
*
* @param string $url File URI in any format. Even `image.jpg`.
*
* @return int Attachment id or 0.
*
* @version 1.2
* @author Kama (wp-kama.ru)
*/
function kama_attachment_url_to_post_id( $url = '' ){
global $wpdb;
$url = filter_var( $url, FILTER_VALIDATE_URL );
if ( $url === false ) {
return 0;
}
$name = basename( $url ); // file name
// remove the thumbnail size (-80x80)
$name = preg_replace( '~-(?:\d+x\d+|scaled|rotated)~', '', $name );
// delete extension
$name = preg_replace( '~\.[^.]+$~', '', $name );
// $name = sanitize_file_name( $name );
$post_name = sanitize_title( $name );
// filter by index field post_name
$sql = $wpdb->prepare(
"SELECT ID, guid FROM $wpdb->posts WHERE post_name LIKE %s AND post_title = %s AND post_type = 'attachment'",
$wpdb->esc_like( $post_name ) .'%', $name
);
$attaches = $wpdb->get_results( $sql );
if( ! $attaches ){
return 0;
}
$attachment_id = reset( $attaches )->ID;
// several are found, let's decide which one to take
if( count( $attaches ) > 1 ){
$url_path = parse_url( $url, PHP_URL_PATH );
foreach( $attaches as $attach ){
if( false !== strpos( $attach->guid, $url_path ) ){
$attachment_id = $attach->ID;
break;
}
}
}
return (int) apply_filters( 'kama_attachment_url_to_post_id', $attachment_id, $url );
}
// Examples of function calls $id = attachment_url_to_post_id( 'http://test-wp.ru/wp-content/uploads/2017/02/Lighthouse.jpg' ); $id = attachment_url_to_post_id( 'wp-content/uploads/2017/02/Lighthouse.jpg' ); $id = attachment_url_to_post_id( 'oads/2017/02/Lighthouse.jpg' );
Notes
- Global. wpdb.
$wpdbWordPress database abstraction object.
Changelog
| Since 4.0.0 | Introduced. |