Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extending-the-rest-api/adding-custom-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Arguments are defined as a map in the key `args` for each endpoint (next to your

* `default`: Used as the default value for the argument, if none is supplied.
* `required`: If defined as true, and no value is passed for that argument, an error will be returned. No effect if a default value is set, as the argument will always have a value.
* `validate_callback`: Used to pass a function that will be passed the value of the argument. That function should return true if the value is valid, and false if not.
* `validate_callback`: Used to pass a function that will be passed the value of the argument. That function should return true if the value is valid, and false if not. Note that `null` values bypass validation.
* `sanitize_callback`: Used to pass a function that is used to sanitize the value of the argument before passing it to the main callback.

Using `sanitize_callback` and `validate_callback` allows the main callback to act only to process the request, and prepare data to be returned using the `WP_REST_Response` class. By using these two callbacks, you will be able to safely assume your inputs are valid and safe when processing.
Expand Down
15 changes: 14 additions & 1 deletion extending-the-rest-api/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,20 @@ Because the WordPress REST API accepts [URL form encoded](https://en.wikipedia.o

When using multiple types, types will be evaluated in the order they are specified. This can have an impact on the sanitized data received by your REST API endpoint. For instance, in the previous example, if the value submitted was `"1"`, it would be sanitized to the boolean `true` value. However, if the order was flipped, the value would remain as the string `"1"`.

[info]The JSON Schema specification allows for defining schemas without a `type` field. The WordPress implementation however requires a `type` to be defined, and will issue a `_doing_it_wrong` notice if a type is ommitted.[/info]
[info]The JSON Schema specification allows for defining schemas without a `type` field. The WordPress implementation however requires a `type` to be defined, and will issue a `_doing_it_wrong` notice if a type is omitted.[/info]

#### Resetting Values

The WordPress REST API uses the value `null` (as in a properly typed `null` sent using JSON) to reset a value to the default for its type. This is accomplished by allowing parameters with a value of `null` to bypass validation. `null` is converted to the following values depending on the type:

- `string` An empty string.
- `number` The value `0.0`.
- `integer` The value `0`.
- `boolean` The value `false`.
- `array` An empty native PHP array.
- `object` An empty native PHP array.

When using multiple types, `null` will not be converted to any value by the sanitization function.

### Format

Expand Down