diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index b094634985..37c697e43f 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -789,6 +789,8 @@ class WP_REST_Server { } if ( ! is_wp_error( $response ) ) { + // Remove the redundant preg_match argument. + unset( $args[0] ); $request->set_url_params( $args ); $request->set_attributes( $handler ); diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 54a81a91c0..93c3a279ed 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -106,6 +106,21 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertArrayNotHasKey( 'foo', (array) $request ); } + public function test_no_zero_param() { + register_rest_route( 'no-zero', '/test', array( + 'methods' => array( 'GET' ), + 'callback' => '__return_null', + 'args' => array( + 'foo' => array( + 'default' => 'bar', + ), + ), + ) ); + $request = new WP_REST_Request( 'GET', '/no-zero/test' ); + $this->server->dispatch( $request ); + $this->assertEquals( array( 'foo' => 'bar' ), $request->get_params() ); + } + /** * Pass a capability which the user does not have, this should * result in a 403 error.