WordPress at a glance

is_local_attachment() WP 1.0

Check if the attachment URI is local one and is really an attachment.

Works based on: url_to_postid()
✈ 1 time = 0.001947s = very slow | 50000 times = 80.64s = 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

Code of is_local_attachment: wp-includes/post.php VER 4.9.8

<?php
function is_local_attachment($url) {
	if (strpos($url, home_url()) === false)
		return false;
	if (strpos($url, home_url('/?attachment_id=')) !== false)
		return true;
	if ( $id = url_to_postid($url) ) {
		$post = get_post($id);
		if ( 'attachment' == $post->post_type )
			return true;
	}
	return false;
}

Related Functions

From tag: Conditional tags (all)

More from category: Attachments (Media files)

No comments
    Hello, !     Log In . Register