Adding Custom Endpoint

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

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.

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

Get all params.

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

Get body from raw json.

$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.

Last updated