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_metadata_from_meta_element() WordPress Method

This function is used to get metadata from a meta element. It returns an array of metadata key value pairs.

WP_REST_URL_Details_Controller::get_metadata_from_meta_element( array $meta_elements, string $attr, string $attr_value ) #

Gets the metadata from a target meta element.


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.

$attr

(string)(Required)Attribute that identifies the element with the target metadata.

$attr_value

(string)(Required)The attribute's value that identifies the element with the target metadata.


Top ↑

Return

(string) The metadata 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_metadata_from_meta_element( $meta_elements, $attr, $attr_value ) {
		// Bail out if there are no meta elements.
		if ( empty( $meta_elements[0] ) ) {
			return '';
		}

		$metadata = '';
		$pattern  = '#' .
				/*
				 * Target this attribute and value to find the metadata element.
				 *
				 * Allows for (a) no, single, double quotes and (b) whitespace in the value.
				 *
				 * Why capture the opening quotation mark, i.e. (["\']), and then backreference,
				 * i.e \1, for the closing quotation mark?
				 * To ensure the closing quotation mark matches the opening one. Why? Attribute values
				 * can contain quotation marks, such as an apostrophe in the content.
				 */
				$attr . '=([\"\']??)\s*' . $attr_value . '\s*\1' .

				/*
				 * These are the options:
				 * - i : case insensitive
				 * - s : allows newline characters for the . match (needed for multiline elements)
				 * - U means non-greedy matching
				 */
				'#isU';

		// Find the metadata element.
		foreach ( $meta_elements[0] as $index => $element ) {
			preg_match( $pattern, $element, $match );

			// This is not the metadata element. Skip it.
			if ( empty( $match ) ) {
				continue;
			}

			/*
			 * Found the metadata element.
			 * Get the metadata from its matching content array.
			 */
			if ( isset( $meta_elements[2][ $index ] ) && is_string( $meta_elements[2][ $index ] ) ) {
				$metadata = trim( $meta_elements[2][ $index ] );
			}

			break;
		}

		return $metadata;
	}


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.