PHP code example of ssnepenthe / simple-wp-routing
1. Go to this page and download the library: Download ssnepenthe/simple-wp-routing 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/ */
ssnepenthe / simple-wp-routing example snippets
use SimpleWpRouting\Exception\NotFoundHttpException;
use SimpleWpRouting\Responder\JsonResponder;
use SimpleWpRouting\Router;
// Create a router instance.
$router = new Router();
// Optional - configure your router via various available setters.
// At a minimum it is recommended to set a route prefix in order to avoid conflicts with core and other plugins.
$router->setPrefix('pfx_');
// Wire your router up with WordPress.
$router->initialize(
// The initialize method accepts a callback which is where you should add all of your routes.
function (Router $router) {
// Routes are registered via HTTP method shortcuts on the router instance.
$route = $router->get(
// Route syntax comes from FastRoute.
'api/users/{user}',
// Route handlers are automatically invoked for their corresponding route/HTTP method pair.
function (array $vars) {
// Handlers receive an array of matched route variables by default.
$user = getUserDataById((int) $vars['user']);
// HTTP exceptions are automatically converted to error responses.
if (null === $user) {
throw new NotFoundHttpException();
}
// Handlers can optionally return a responder instance.
return new JsonResponder($user);
}
);
// Routes can optionally be configured with an active callback.
$route->setIsActiveCallback(function () {
// Return true to enable this route, false to disable it.
return isApiUserEndpointEnabled();
});
}
);
use SimpleWpRouting\NotFoundHttpException;
$router->get('books/{book}', function (array $vars) {
if (! $book = getBookById($vars['book'])) {
throw new NotFoundHttpException();
}
// ...
});
use SimpleWpRouting\Responder\JsonResponder;
$router->get('api/products', function () {
return new JsonResponder(['products' => getAllProducts()]);
});
use SimpleWpRouting\Responder\QueryResponder;
$router->get('products/random[/{count}]', function (array $vars) {
$count = (int) ($vars['count'] ?? 5);
return new QueryResponder([
'post_type' => 'pfx_product',
'orderby' => 'rand',
'posts_per_page' => clamp($count, 1, 10),
]);
});
use SimpleWpRouting\Responder\RedirectResponder;
$router->get('r/{redirect}', function (array $vars) {
$location = getRedirectLocationById($vars['redirect']);
return new RedirectResponder($location);
});
return new RedirectResponder($location, 302, 'WordPress', false);
use SimpleWpRouting\Responder\TemplateResponder;
$router->get('thank-you', function () {
return new TemplateResponder(__DIR__ . '/templates/thank-you.php');
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.