is_local_attachment()
Check if the attachment URI is local one and is really an attachment.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Uses: url_to_postid()
1 time — 0.001947 sec (very slow) | 50000 times — 80.64 sec (very slow) | PHP 7.0.5, WP 4.4.2
No Hooks.
Return
true|false
. True on success, false on failure.
Usage
is_local_attachment( $url );
- $url(string) (required)
- URL to check
Examples
#1 Check the URL to see if this is an attachment
Let's say we have a link to a file in the wordpress media library: http://example.com/wp-content/uploads/2016/03/import.png
and this file also has its own page in the frontend: http://example.com/import
:
is_local_attachment( 'http://example.com/wp-content/uploads/2016/03/import.png' ); //> false is_local_attachment( 'http://example.com/import' ); //> true // nonexistent file is_local_attachment( 'http://example.com/fooo' ); //> false
Changelog
Since 2.0.0 | Introduced. |
is_local_attachment() is local attachment code WP 6.8
function is_local_attachment( $url ) { if ( ! str_contains( $url, home_url() ) ) { return false; } if ( str_contains( $url, home_url( '/?attachment_id=' ) ) ) { return true; } $id = url_to_postid( $url ); if ( $id ) { $post = get_post( $id ); if ( 'attachment' === $post->post_type ) { return true; } } return false; }