# \[API] Một ví dụ sử dụng đối số Arguments (ok)

![](/files/-MOAfYQfCYZqfArdvBYb)

![](/files/-MOAf_aOekZlvdRvmeER)

```
<?php
/**
 * This is our callback function that embeds our resource in a WP_REST_Response
 */
function prefix_get_colors($request) {
  // In practice this function would fetch the desired data. Here we are just making stuff up.
  $colors = array(
    'blue',
    'blue',
    'red',
    'red',
    'green',
    'green',
  );
  if (isset($request['filter'])) {
    $filtered_colors = array();
    foreach ($colors as $color) {
      if ($request['filter'] === $color) {
        $filtered_colors[] = $color;
      }
    }
    return rest_ensure_response($filtered_colors);
  }
  return rest_ensure_response($colors);
}
/**
 * We can use this function to contain our arguments for the example product endpoint.
 */
function prefix_get_color_arguments() {
  $args = array();
  // Here we are registering the schema for the filter argument.
  $args['filter'] = array(
    // description should be a human readable description of the argument.
    'description' => esc_html__('The filter parameter is used to filter the collection of colors', 'my-text-domain'),
    // type specifies the type of data that the argument should be.
    'type'        => 'string',
    // enum specified what values filter can take on.
    'enum'        => array('red', 'green', 'blue'),
  );
  return $args;
}
/**
 * This function is where we register our routes for our example endpoint.
 */
function prefix_register_example_routes() {
  // register_rest_route() handles more arguments but we are going to stick to the basics for now.
  register_rest_route('my-colors/v1', '/colors', array(
    // By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
    'methods'  => WP_REST_Server::READABLE,
    // Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
    'callback' => 'prefix_get_colors',
    // Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
    'args'     => prefix_get_color_arguments(),
  ));
}
add_action('rest_api_init', 'prefix_register_example_routes');
?>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/wordpress/api-mot-vi-du-su-dung-doi-so-arguments-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
