PHP code example of weblove / wp-router
1. Go to this page and download the library: Download weblove/wp-router library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
weblove / wp-router example snippets
$router = new Weblove\WPRouter\Router;
$router->get("/meme/:id", function ($request, $response) {
$params = $request->get_url_params();
$the_meme = get_meme_by_id($params["id"]);
$response["body"] = $the_meme;
return $response;
});
$myCustomMiddleware = function ($req, $res) {
$potato_id = $req->get_param("id");
$res["potato"] = get_potato($potato_id);
return $res;
};
$myCustomAuthMiddleware = function ($req, $res) {
$can_touch_this = user_can_touch_this();
if (!$can_touch_this) {
return new WP_REST_Response("Can't touch this", 403);
}
return $res;
};
$router->get("/meme/:id/category",
function ($req, $res)
{
$params = $req->get_url_params();
$the_meme = get_meme_by_id($params["id"]);
$res["body"] = $the_meme;
return $res;
},
function ($req, $res)
{
$meme_category_id = $res["body"]["category"]["id"];
$meme_category_infos = get_meme_cat_infos($meme_category_id);
$res["body"] = $meme_category_infos;
return $res;
}
);
$router->hook('GET', '/wp/v2/posts/:id',
$authMiddleware,
$responseMiddleware // this function ends by returning an instance of WP_REST_Response
);
$router = new Weblove\WPRouter\Router; // defaults to /wp-json/api
$router = new Weblove\WPRouter\Router("custom"); // now set to /wp-json/custom