Get an attachment ID by URL in WordPress
We all know you can use wp_get_attachment_url() to return an attachment’s URL by passing in the ID, but what about the reverse scenario?
There are a lot of long-winded examples of how to get an attachment ID by URL floating around the interwebs. Most of them limit results to only returning images or use expensive DB queries. Really, there should be a function for this in core, but there isn’t.
Needless to say, I wasn’t really happy with any of the solutions I found people using, so I decided to take a stab at it.
Below is the most lightweight method I’ve come up with (so far) to get an attachment ID by passing through an attachment URL.
Example usage
Just as an example, this would echo the attachment ID integer of test-image.jpg onto the page:
<?php echo fjarrett_get_attachment_id_by_url( 'http://frankiejarrett.com/wp-content/uploads/2013/05/test-image.jpg' ) ?> |
This would echo the same result as above because we are ignoring www usage in the URL being passed through:
<?php echo fjarrett_get_attachment_id_by_url( 'http://www.frankiejarrett.com/wp-content/uploads/2013/05/test-image.jpg' ) ?> |
This would echo nothing, because the image doesn’t exist in the WordPress uploads directory:
<?php echo fjarrett_get_attachment_id_by_url( 'http://frankiejarrett.com/foo/test-image.jpg' ) ?> |
And finally, this would also echo nothing because the URL of the file is pointing to an external domain:
<?php echo fjarrett_get_attachment_id_by_url( 'http://www.anotherdomain.com/wp-content/uploads/2013/05/test-image.jpg' ) ?> |
Conclusion
We managed to fetch an attachment ID in just a few lines of code with no expensive DB queries! Awesome ![]()
Now you can do cool things like turn a URL into an absolute path:
<?php $url = 'http://frankiejarrett.com/wp-content/uploads/2013/05/test-image.jpg'; $attachment_id = fjarrett_get_attachment_id_by_url( $url ); $path = ( $attachment_id ) ? get_attached_file( $attachment_id ) : null; echo $path; ?> |
For more information about what was used in this function, please see:
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.parse-url.php
http://php.net/manual/en/function.str-ireplace.php
http://codex.wordpress.org/Function_Reference/home_url
http://codex.wordpress.org/Determining_Plugin_and_Content_Directories#Constants
http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column
Was this code helpful to you? Let me know in the comments!












Weston Ruter 7:20 pm on May 8, 2013 Permalink |
What’s the use case for this function? I’m trying to think of why I’d need to look up a post associated with an uploaded file. Is it to obtain the attachment ID for an image that has been inserted into a post, one for which the ID-containing class name has been stripped?
Frankie Jarrett 8:48 pm on May 8, 2013 Permalink |
Hey Weston, yeah that’s one possible use case for it.
In one of my themes I have a post meta field where users can enter a URL to a file, either one from the Media Library or an arbitrary one hosted elsewhere.
By using this function we can determine whether or not the file in question has an attachment ID, and if so, the path can be retrieved using
get_attached_file( $attachment_id )to then pass inside a PHP force download script usingreadfile()in order to create a “Download File” link.I plan to post that force download functionality in its entirety in my next tutorial.
Rafal 7:05 am on May 12, 2013 Permalink
I personally used a similar snippet found on stackexchange to get couple of different image sizes based on a single URL.
I guess that adding wp cache to the db query could improve the snippet a bit.
Jeremy Ross 12:17 am on May 20, 2013 Permalink |
Is it safe to search the guid field? If someone changes the permalinks, or migrates urls without updating the guid field, this wouldn’t return anything even if the file actually existed.
Frankie Jarrett 10:05 am on May 20, 2013 Permalink |
Hey Jeremy! Thanks for your question.
Changing permalinks on attachments in the WP Admin is not possible AFAIK. Even if you could, the GUID uses the actual file URL and not the permalink, which is a shortened URL using the
attachment_idquery string.Attachment media is the one exception to the “never touch GUID” rule. Since it uses the URL as the GUID it actually should be updated to the new hostname after a migration.
However, the DB query in this function is using
RLIKEto find a partial string match starting with the wp-content directory and ignores the hostname completely. E.g.wp-content/uploads/2013/05/test-image.jpgSo even if you fail to update the GUID on attachments during a hostname migration, this function will still work (as long as the uploads directory hasn’t also changed).