Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

WP_REST_URL_Details_Controller::get_image() WordPress Method

The WP_REST_URL_Details_Controller::get_image() function is used to get the image associated with a URL. This function will return an array containing the image URL, width, and height. If no image is associated with the URL, an empty array will be returned.

WP_REST_URL_Details_Controller::get_image( array $meta_elements, string $url ) #

Parses the Open Graph (OG) Image from the provided HTML.


Description

See: https://ogp.me/.


Top ↑

Parameters

$meta_elements

(array)(Required)A multi-dimensional indexed array on success, else empty array.

  • (string[]) Meta elements with a content attribute.
  • '1'
    (string[]) Content attribute's opening quotation mark.
  • '2'
    (string[]) Content attribute's value for each meta element.

$url

(string)(Required)The target website URL.


Top ↑

Return

(string) The OG image on success. Empty string if not found.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php

	private function get_image( $meta_elements, $url ) {
		$image = $this->get_metadata_from_meta_element(
			$meta_elements,
			'property',
			'(?:og:image|og:image:url)'
		);

		// Bail out if image not found.
		if ( '' === $image ) {
			return '';
		}

		// Attempt to convert relative URLs to absolute.
		$parsed_url = parse_url( $url );
		if ( isset( $parsed_url['scheme'] ) && isset( $parsed_url['host'] ) ) {
			$root_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . '/';
			$image    = WP_Http::make_absolute_url( $image, $root_url );
		}

		return $image;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.