Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created August 6, 2015 20:15
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpscholar/3b00af01863c9dc562e5 to your computer and use it in GitHub Desktop.
Save wpscholar/3b00af01863c9dc562e5 to your computer and use it in GitHub Desktop.
<?php
/**
* Get an attachment ID given a URL.
*
* @param string $url
*
* @return int Attachment ID on success, 0 on failure
*/
function get_attachment_id( $url ) {
$attachment_id = 0;
$dir = wp_upload_dir();
if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory?
$file = basename( $url );
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'value' => $file,
'compare' => 'LIKE',
'key' => '_wp_attachment_metadata',
),
)
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$original_file = basename( $meta['file'] );
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
$attachment_id = $post_id;
break;
}
}
}
}
return $attachment_id;
}
@jonathanstanley
Copy link

Thank you very much for this. I'm not sure if WP has changed since this post, but I thought I'd share some of my changes:

WordPress now also creates backup files. See wp_restore_image. One way to catch them is with this modification around line 35-51:

if ($query->have_posts()) { 
  foreach ($query->posts as $post_id) {
    $meta = wp_get_attachment_metadata($post_id);
    $source_file            = basename($meta['file']);
    $original_file          = basename($meta['original_image']);
    $cropped_image_files    = wp_list_pluck($meta['sizes'], 'file');

    if ($source_file === $file || $original_file === $file || in_array($file, $cropped_image_files)) {
      return $post_id;
    }
  }
}

similarly / alternatively:

$query_args = array(
  'post_type'   => 'attachment',
  'post_status' => 'inherit',
  'fields'      => 'ids',
  'meta_query'  => array(
    array(
      'value' => $file,
      'compare' => 'LIKE',
      'key' => '_wp_attachment_backup_sizes'
    ),
  )
);

$query = new WP_Query($query_args);

if ($query->have_posts()) {
    foreach ($query->posts as $post_id) {
        $meta   = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
        $files  = wp_list_pluck($meta, 'file');
        if (in_array($file, $files)) {
            return $post_id;
        }
    }
}

In my particular case, I wanted to check for everything, including pdfs. So here is a much broader (and slower) search

$query_args = array(
  'post_status' => 'any',
  'post_type'   => 'attachment',
  'fields'      => 'ids',
  'meta_query'  => array(
    array(
      'value'   => $file,
      'compare' => 'LIKE',
    ),
  )
);

$query = new WP_Query($query_args);

if ($query->have_posts()) {
  return $query->posts[0]; //this assumes the first post ID is correct
}
return 0;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment