WP_REST_Controller::get_context_param() WordPress Method

The WP_REST_Controller::get_context_param() method is used to get the value of a specific context parameter from the current request.

WP_REST_Controller::get_context_param( array $args = array() ) #

Retrieves the magical context param.


Description

Ensures consistent descriptions between endpoints, and populates enum from schema.


Top ↑

Parameters

$args

(array)(Optional) Additional arguments for context parameter.

Default value: array()


Top ↑

Return

(array) Context parameter details.


Top ↑

Source

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

	public function get_context_param( $args = array() ) {
		$param_details = array(
			'description'       => __( 'Scope under which the request is made; determines fields present in response.' ),
			'type'              => 'string',
			'sanitize_callback' => 'sanitize_key',
			'validate_callback' => 'rest_validate_request_arg',
		);

		$schema = $this->get_item_schema();

		if ( empty( $schema['properties'] ) ) {
			return array_merge( $param_details, $args );
		}

		$contexts = array();

		foreach ( $schema['properties'] as $attributes ) {
			if ( ! empty( $attributes['context'] ) ) {
				$contexts = array_merge( $contexts, $attributes['context'] );
			}
		}

		if ( ! empty( $contexts ) ) {
			$param_details['enum'] = array_unique( $contexts );
			rsort( $param_details['enum'] );
		}

		return array_merge( $param_details, $args );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.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.