> For the complete documentation index, see [llms.txt](https://githubichsan.gitbook.io/journal/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://githubichsan.gitbook.io/journal/wordpress-development/rest-api/adding-custom-endpoint.md).

# Adding Custom Endpoint

This will be return `makan bang` string response in `localhost/wp-json/contoh/v1/makan`in method get.

```php
function my_awesome_func($data)
{
	return "makann bang";
}

add_action('rest_api_init', function () {
	register_rest_route('contoh/v1', '/makan', array (
		'methods' => 'GET',
		'callback' => 'my_awesome_func',
	));
});
```

Get params.

```php
function my_awesome_func(WP_REST_Request $request)
{
	$param = $request['code'];
	return ["ko" => $param];
}
```

Get all params.

```php
function my_awesome_func(WP_REST_Request $request)
{
	$a = $request->get_query_params();
	return ["ko" => $a];
}
```

Get body from raw json.

```php
$a = $request->get_json_params();
```

If the request has the `Content-type: application/json` header set and valid JSON in the body, `get_json_params()` will return the parsed JSON body as an associative array.
