Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

Comment on WP_REST_Request::get_param() by Rodrigo Vieira Eufrasio da Silva

Дата публикации: 14-08-2026 17:27:40

get_param() checks parameter sources in this priority order (from get_parameter_order()): JSON body > POST body > GET query string > URL route params > registered defaults. The first source that HAS the key wins — even if that source is less "authoritative" than you'd expect.
The gotcha: parameters captured by your route's regex (e.g. the id in '/wpdocs/v1/users/(?P\d+)') are checked SECOND TO LAST, after the query string. So a request to:
/wp-json/wpdocs/v1/users/5?id=999
...will have $request->get_param( 'id' ) return 999 (from the query string), NOT 5 (from the URL route). The same applies to a JSON body containing {"id": 999} sent to that same URL.
This matters if your code reads the same key in two different places and assumes they'll agree — for example, doing a permission check against $request->get_url_params()['id'] (or the URL structure itself) but then using $request->get_param( 'id' ) for the actual database operation. Since the two can diverge, this pattern can lead to acting on a different resource than the one that was permission-checked.
To avoid this, be explicit about which source you actually mean:
// Only trust what the route regex captured, ignore query/body entirely.
$id = $request->get_url_params()['id'] ?? null;
// Or, if you specifically want query-string input, be explicit:
$id = $request->get_query_params()['id'] ?? null;
Reserve get_param() for cases where you genuinely want "whichever source provides this key first," and use the specific getter (get_url_params(), get_query_params(), get_body_params(), get_json_params()) whenever the source matters — especially for any value used in a permission check or database query.

Основное содержимое страницы с новостью.

Retrieves a parameter from the request.

Parameters
$keystringrequired

Parameter name.

Return mixed|null Value if set, null otherwise. Source
public function get_param( $key ) {
	$order = $this->get_parameter_order();

	foreach ( $order as $type ) {
		// Determine if we have the parameter for this type.
		if ( isset( $this->params[ $type ][ $key ] ) ) {
			return $this->params[ $type ][ $key ];
		}
	}

	return null;
}

View all references View on Trac View on GitHub

UsesDescription
WP_REST_Request::get_parameter_order()wp-includes/rest-api/class-wp-rest-request.php

Retrieves the parameter priority order.

Used byDescription
WP_REST_Request::has_valid_params()wp-includes/rest-api/class-wp-rest-request.php

Checks whether this request is valid according to its attributes.

WP_REST_Request::offsetGet()wp-includes/rest-api/class-wp-rest-request.php

Retrieves a parameter from the request.

Changelog
VersionDescription
4.4.0Introduced.
User Contributed Notes
  1. Skip to note 2 content

    get_param() checks parameter sources in this priority order (from get_parameter_order()): JSON body > POST body > GET query string > URL route params > registered defaults. The first source that HAS the key wins — even if that source is less “authoritative” than you’d expect.

    The gotcha: parameters captured by your route’s regex (e.g. the id in ‘/wpdocs/v1/users/(?P\d+)’) are checked SECOND TO LAST, after the query string. So a request to:

    /wp-json/wpdocs/v1/users/5?id=999

    …will have $request->get_param( ‘id’ ) return 999 (from the query string), NOT 5 (from the URL route). The same applies to a JSON body containing {“id”: 999} sent to that same URL.

    This matters if your code reads the same key in two different places and assumes they’ll agree — for example, doing a permission check against $request->get_url_params()[‘id’] (or the URL structure itself) but then using $request->get_param( ‘id’ ) for the actual database operation. Since the two can diverge, this pattern can lead to acting on a different resource than the one that was permission-checked.

    To avoid this, be explicit about which source you actually mean:

    // Only trust what the route regex captured, ignore query/body entirely.
    $id = $request->get_url_params()[‘id’] ?? null;

    // Or, if you specifically want query-string input, be explicit:
    $id = $request->get_query_params()[‘id’] ?? null;

    Reserve get_param() for cases where you genuinely want “whichever source provides this key first,” and use the specific getter (get_url_params(), get_query_params(), get_body_params(), get_json_params()) whenever the source matters — especially for any value used in a permission check or database query.

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1Внутренние запросы к WordPress REST API08.1908-08-2026
2 Comment on WP_Icons_Registry by Rodrigo Vieira Eufrasio da Silva 09.3814-08-2026
3 Comment on wp_insert_post by Rodrigo Vieira Eufrasio da Silva 013.1214-08-2026
4 Comment on resolve_pattern_blocks() by Rodrigo Vieira Eufrasio da Silva 010.6514-08-2026
5 Comment on load_script_textdomain_relative_path by Rodrigo Vieira Eufrasio da Silva 08.6914-08-2026
6 Comment on load_script_module_textdomain() by Rodrigo Vieira Eufrasio da Silva 06.9814-08-2026
7 Comment on wp_deregister_script() by Rodrigo Vieira Eufrasio da Silva 029.5314-08-2026
8WordPress REST API: Top Benefits for Web Developers012.3718-08-2026
9PHP Type Juggling: как нестрогое сравнение превращается в обход аутентификации08.122-07-2026
10Add a JSON API to Any Static Site Generator06.8323-04-2026

Классификация: . Схожих патентов: 0. Схожих новостей: 10. Тональность: 0. Информативность: 12.64. Источник: developer.wordpress.org.