WPSEO_Image_Utils::get_attachment_by_url()public staticYoast 1.0

Find an attachment ID for a given URL.

Метод класса: WPSEO_Image_Utils{}

Хуков нет.

Возвращает

int. The found attachment ID, or 0 if none was found.

Использование

$result = WPSEO_Image_Utils::get_attachment_by_url( $url );
$url(строка) (обязательный)
The URL to find the attachment for.

Код WPSEO_Image_Utils::get_attachment_by_url() Yoast 22.3

public static function get_attachment_by_url( $url ) {
	/*
	 * As get_attachment_by_url won't work on resized versions of images,
	 * we strip out the size part of an image URL.
	 */
	$url = preg_replace( '/(.*)-\d+x\d+\.(jpg|png|gif)$/', '$1.$2', $url );

	static $uploads;

	if ( $uploads === null ) {
		$uploads = wp_get_upload_dir();
	}

	// Don't try to do this for external URLs.
	if ( strpos( $url, $uploads['baseurl'] ) !== 0 ) {
		return 0;
	}

	if ( function_exists( 'wpcom_vip_attachment_url_to_postid' ) ) {
		// @codeCoverageIgnoreStart -- We can't test this properly.
		return (int) wpcom_vip_attachment_url_to_postid( $url );
		// @codeCoverageIgnoreEnd -- The rest we _can_ test.
	}

	return self::attachment_url_to_postid( $url );
}