1. Go to this page and download the library: Download em4nl/unplug 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/ */
em4nl / unplug example snippets
\Unplug\unplug();
// No need to WordPress will always run that before
// `index.php`.
use Em4nl\Unplug;
// Basically, just use the `_use`, `get` and `post` functions to
// set up your frontend routes.
// _use callbacks will be called on all routes. Use this to add
// stuff to your context that you always need. For example a global
// image for sharing your website on facebook and twitter, loaded
// from an ACF field:
Unplug\_use(function(&$context) {
$context['share_image'] = get_field('share_image', 'options');
});
// You can have as many _use callbacks as you like. Another example
// would be to add a menu
Unplug\_use(function(&$context) {
$context['menu'] = array(/* TODO load this from WordPress */);
});
// If you don't want to mutate the $context array directly, you can
// also return a changed copy.
// If you want to have the Twig template engine available on every
// route, why not store it in the $context
Unplug\_use(function($context) {
$twig_loader = new Twig_Loader_Filesystem(
get_template_directory() . '/templates'
);
$twig = new Twig_Environment($twig_loader, [
'debug' => true,
]);
$twig->addExtension(new Twig_Extension_Debug());
$context['twig'] = $twig;
return $context;
});
// The index route. Return a string to send html. Especially useful
// in conjunction with Twig
Unplug\get('/', function($context) {
return $context['twig']->render('home.twig', $context);
});
// Or just echo your response
Unplug\get('/hi-world', function($context) {
echo "Hello world!";
});
// Return an array to automatically send a json response
Unplug\get('/api', function($context) {
return ['error' => NULL];
});
// Routes can have parameters; the parameter values are collected
// into the $context['params'] array
Unplug\get('/:param', function($context) {
return "Hi, {$context['params'][0]}!";
});
// Routes can have optional path segments. To find out wether
// they're present, examine $context['path']
Unplug\get('/menu/open?', function($context) {
return "You're visiting {$context['path']}";
});
// Routes can also have wildcards that match any number of segments
// The content of the wildcard is also exposed in
// $context['params']
Unplug\get('/test/*', function($context) {
return "The wildcard path: {$context['params'][0]}";
});
// You can also have POST routes. Unplug doesn't take care of the
// posted data for you; just use the $_POST array for that
// (Same goes for query parameters in $_GET)
Unplug\post('/form', function($context) {
// ...
});
// If you want to return something other than a '200 OK' (or a 200
// _explicitly_), you can use the `Unplug\ok`, `Unplug\not_found`,
// `Unplug\moved_permanently` and `Unplug\found` functions.
function my_404($context) {
Unplug\not_found($context['twig']->render('error_404.twig', $context));
}
// Sometimes you have a parametrised route where not all
// parameter values are valid. This can be handled e.g. like this
Unplug\get('/post/:title', function($context) {
$context['post'] = my_get_post($context['params'][0]);
if ($context['post']) {
return $context['twig']->render('post.twig', $context);
} else {
my_404($context);
}
});
// You'll almost always want to register a global catchall callback
// that will be used if no other route matches where you deliver
// your 404 page.
Unplug\catchall('my_404');
// After all routes are set up, you still have to call this, or
// nothing will be run!
Unplug\dispatch();