WP_REST_Request::get_url_params(): array

Retrieves parameters from the route itself.

Description

These are parsed from the URL using the regex.

Return

array Parameter map of key to value.

Source

public function get_url_params() {
	return $this->params['URL'];
}

Changelog

VersionDescription
4.4.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Return a custom field with featured the image html and set image size based on URL params:

    add_action( 'rest_api_init', function() {
        register_rest_field( 'post', 'featured_image', array(
            'get_callback' => 'wporg_get_rest_featured_image',
        ) );
    } );
    
    function wporg_get_rest_featured_image( $object, $attr, $request ) {
    	$image_size = $request->get_url_params( 'id' ) ? 'full' : 'thumbnail';
    	return get_the_post_thumbnail( $object['id'], $image_size );
    }

You must log in before being able to contribute a note or feedback.