Routing to callback functions in WordPress - 02
May 29, 2014
I do not use Laravel and have never built anything significant with it; I've just seen its code used here and here in Tuts+ tutorials and really liked how clean, on the developer side, it is.
I'm just applying the most basic version of admiration: copying.
I've created a wrapper around WP Router plugin class to allow Laravel-like routing in WordPress.
The wrapper is limited in its workings and offers just some basic functions right now; mine is not the will to port Laravel Route
to WordPress but to wrap WP Router function calling into a more fluent interface that's all the latest fury.
What it does not
Peeking Laravel routing documentation the first example is the root path
Route::get('/', function()
{
return 'Hello World';
});
WP Router equivalent, though,
$router->add_route('root', array('path' => '/', 'page_callback' => function ()
{
echo "Hello World";
}, 'template' => false));
will not work due to the fact that WordPress built-in .htaccess
file (on Apache server) will redirect all requests for the root path to the index.php
file using the one defined by the active theme.
What it does
Setting up a path like /hello
using WP Router requires, in it's most basic form, this code
$router->add_route('hello', array('path' => '^hello$', 'page_callback' => function ()
{
echo "Hello";
}, 'template' => false));
to return a blank page containing the result of just that echo
statement [caption id="attachment_1040" align="aligncenter" width="451"][](http://theaveragedev.local/wordpress/wp-content/uploads/2014/05/2014-05-29-at-14.59.png) Hello page[/caption] Because the falsy
template
argument WP Router will not use any template to render the content.
The same code would instead set the page content and let the current theme show it as it sees fit if the template
argument is left alone.
$router->add_route('hello', array('path' => '^hello$', 'page_callback' => function ()
{
return "Hello";
}));
on Twentyfourteen that would be [caption id="attachment_1041" align="aligncenter" width="1024"][](http://theaveragedev.local/wordpress/wp-content/uploads/2014/05/2014-05-29-at-15.04.png) Hello Twenty Fourteen[/caption] Hence the Laravel like behaviour where all the heavy-lifting of the page rendering is left to the callback function is obtained passing a falsy
template
argument.
WP Router allows passing more callbacks when template != false
; I'm not delving in the specs as it's all written in the plugin notes.
Some parameters
I'd like the hello
route to be smarter and be able to salute using the name
parameter (query var), WP Router allows that using
$router->add_route('hello', array(
'path' => '^hello/(\w+?)$',
'query_vars' => array(
'name' => 1,
),
'page_callback' => function($name) { echo "Hello $name"; },
'page_arguments' => array('name'),
'template' => false
));
to print something as stunning as this when visiting the /hello/Luca
page [caption id="attachment_1042" align="aligncenter" width="563"][](http://theaveragedev.local/wordpress/wp-content/uploads/2014/05/2014-05-29-at-16.19.png) Hello me[/caption] The same output using the theme
page.php
template would be
$router->add_route('hello', array(
'path' => '^hello/(\w+?)$',
'query_vars' => array(
'someName' => 1,
),
'page_callback' => function($someName){echo "Hello $someName";},
'page_arguments' => array('someName'),
'access_callback' => TRUE,
'title' => 'Saluting Page',
'template' => array('page.php')
));
Wrapping it up
Nailed these basic working the very first implentation of the wrapping class would reduce this code
add_action('wp_router_generate_routes', 'addRoutes');
function addRoutes(WP_Router $router)
{
$router->add_route('hello', array(
'path' => '^hello/(\w+?)$',
'query_vars' => array(
'name' => 1,
),
'page_callback' => function($name)
{
echo "Hello $name";
},
'page_arguments' => array('name'),
'template' => false
));
}
to this
function addRoutes()
{
// the Route class will take care of hooking
tad\wrappers\WP_Router\Route::get('hello/{name}', function ($name)
{
echo "Hello $name";
})->where('name', '\w+?');
}
Next
The wrapper is still a long way to completion and I would like it to expose all of WP Router interface using Laravel method names and chaining mechanism.