WP_REST_Request::from_url

Advertisement

Summery Summery

Retrieves a WP_REST_Request object from a full URL.

Syntax Syntax

WP_REST_Request::from_url( string $url )

Parameters Parameters

$url

(Required) URL with protocol, domain, path and query args.

Return Return

(WP_REST_Request|false) WP_REST_Request object on success, false on failure.

Source Source

File: wp-includes/rest-api/class-wp-rest-request.php

		// Remove the offset from every group.
		foreach ( $order as $type ) {
			unset( $this->params[ $type ][ $offset ] );
		}
	}

	/**
	 * Retrieves a WP_REST_Request object from a full URL.
	 *
	 * @since 4.5.0
	 *
	 * @param string $url URL with protocol, domain, path and query args.
	 * @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
	 */
	public static function from_url( $url ) {
		$bits         = parse_url( $url );
		$query_params = array();

		if ( ! empty( $bits['query'] ) ) {
			wp_parse_str( $bits['query'], $query_params );
		}

		$api_root = rest_url();
		if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
			// Pretty permalinks on, and URL is under the API root.
			$api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
			$route        = parse_url( $api_url_part, PHP_URL_PATH );
		} elseif ( ! empty( $query_params['rest_route'] ) ) {
			// ?rest_route=... set directly.
			$route = $query_params['rest_route'];
			unset( $query_params['rest_route'] );
		}

		$request = false;
		if ( ! empty( $route ) ) {
			$request = new WP_REST_Request( 'GET', $route );

Advertisement

Changelog Changelog

Changelog
Version Description
4.5.0 Introduced.

Advertisement

Leave a Reply