Routing to callback functions in WordPress – 08

A quick recap

After wrapping the functions provided by WP Router plugin into Laravel-like methods, merely a mocking of it, I’ve gone on from that point and reasoned about WordPress integration.
Being able to define routes and endpoints at code level is nice and might be a solution in some context but I do not want to break WordPress UI paradigm and patterns not exposing those routes in a clear, transparent and usable way.

Showing the routes

To obtain that latter result a quick workaround might be to instruct the client, or the theme/plugin user, to create custom links in navigation menus to, as an example, the /posts relative paths but that’s in my opinion as poor a solution as the standard one to create an empty page and apply an “Archive of posts” (or whatever) template to it.
Furthermore what’s lost in the first solution is the possibility for the user to assign meta information to the page, like a featured image, and for the developer to use it without resorting to misplaced and convoluted practices.
So I’ve decided to create a plugin that will scan the publishable routes like /posts (an ability bestowed to the routes by the developer in the code) and programmatically add pages to the site with proper title and permalink. Those pages will be as user editable as the developer will deem appropriate (giving the user the possibility to edit a title or content that’s hardcoded is just silly and frustrating).

The code-level possibility

To decouple the code and classes wrapping WP Router functions from a pluing acting on those information I’ve added, in the wp-routing package, the PersistableRoute class (extending the Route class) to allow, as the name implies, easy optional persistence of added routes in the database along with some meta information. The idea is that, in its most basic form, I will be able to write code like

PersistableRoute::get('posts', $callback)
    ->withTitle('Archive of posts'); 

to add a route respoding to a GET /posts request be saved in WordPress database with a title of “Archive of posts”. The plugin I’m working on would use that information to programmatically add partially editable pages to the site and show the user something like this: [caption id=“attachment_1073” align=“aligncenter” width=“1024”]The /posts route on the front-end The /posts route on the front-end[/caption]