wp_image_file_matches_image_meta

函数
wp_image_file_matches_image_meta ( $image_location, $image_meta, $attachment_id = 0 )
参数
  • (string) $image_location The full path or URI to the image file.
    Required:
  • (array) $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'.
    Required:
  • (int) $attachment_id Optional. The image attachment ID. Default 0.
    Required:
返回值
  • (bool) Whether the image meta is for this image file.
定义位置
相关方法
wp_image_matches_ratiowp_get_image_mime_wp_get_image_size_from_metawp_read_image_metadatawp_get_attachment_metadata
引入
5.5.0
弃用
-

wp_image_file_matches_image_meta: 这个函数用来检查一个图像文件是否与它相关的图像元数据匹配。它需要两个参数,图像文件的路径和图像元数据,如果图像文件与元数据匹配,则返回true,否则返回false。

确定图像元数据是否用于图像源文件。

图像元数据由附件文章ID检索。在某些情况下,文章ID可能会更改。例如,当网站在另一个网站上导出和导入时。然后,导出网站post_content中的附件文章ID可能与新网站中的相同附件不匹配。

function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
	$match = false;

	// Ensure the $image_meta is valid.
	if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
		// Remove query args in image URI.
		list( $image_location ) = explode( '?', $image_location );

		// Check if the relative image path from the image meta is at the end of $image_location.
		if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
			$match = true;
		} else {
			// Retrieve the uploads sub-directory from the full size image.
			$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );

			if ( $dirname ) {
				$dirname = trailingslashit( $dirname );
			}

			if ( ! empty( $image_meta['original_image'] ) ) {
				$relative_path = $dirname . $image_meta['original_image'];

				if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
					$match = true;
				}
			}

			if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
				foreach ( $image_meta['sizes'] as $image_size_data ) {
					$relative_path = $dirname . $image_size_data['file'];

					if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
						$match = true;
						break;
					}
				}
			}
		}
	}

	/**
	 * Filters whether an image path or URI matches image meta.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $match          Whether the image relative path from the image meta
	 *                               matches the end of the URI or path to the image file.
	 * @param string $image_location Full path or URI to the tested image file.
	 * @param array  $image_meta     The image meta data as returned by 'wp_get_attachment_metadata()'.
	 * @param int    $attachment_id  The image attachment ID or 0 if not supplied.
	 */
	return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
}

常见问题

FAQs
查看更多 >