PHP WordPress

WordPress - Adding Custom API Rest Endpoints

291 days ago

Adds custom API endpoints/requests that you can call using Ajax.

<?php

	public function add_rest_routes()
	{
		register_rest_route("fresh-woo-search/v1", "search", [
			"methods" => "POST",
			"callback" => [$this, "search_products"],
		]);
	}
    
    public function search_products()
	{
		$time_pre = microtime(true);
		wp_send_json(["results" => $items, "time" => $exec_time]);
		exit();
	}
    
    add_action(
		"rest_api_init",
		"add_rest_routes"
	);
    
?>

<script>
	$.post("/wp-json/fresh-woo-search/v1/search", data, function (response) {
    	console.log(response.results)
    })
</script>
Dylan Hoogeveen Dylan Hoogeveen