Routing to callback functions in WordPress – 05

Why go all the pain of adding Laravel-like routing in WordPress if the end-user possibilites are not the same as in any WordPress installation?
Adding a /posts route to show all the posts is nice and easy for a fiddler/hacker/developer but the long term user experience of adding a custom URL to the menus pointing to the /posts path breaks the user right to total ignorance of inner workings.
I’d like to be able to add routes defined in the code to the Menus admin screen for the user to pick and add to the menus with ease.

An example

If I add the following route in code

Route::get('posts', function(){

        // and here I echo/return the list of posts 


I’d like the user to be able to choose the posts path in the Menus admin using something like this

Routes to menus
Routes to menus

The same result would be obtained using templates but I’ve found that’s not so easy to grasp for many clients and I will get called any time the need arise to rename/remove/edit an archive page.
Having the same template assigned to more than one page seems to create a lot of confusion too.

In the above scenario the problem is twofold:

  • Routes defined via WP Router need to be persisted to the database to have meta data (a description at least) associated to them and allow asyinc fetching
  • A metabox populated with such data needs to be added to the Menus admin page

The second condition seems like the most stringent one as the road is more charted and rigid and the game has to be played by WordPress rules.

Metabox possibility

The solution above takes the responsibility of page title and presentation away from the user casting it all on the developer but a meta-box and per-page solution is possible.
I’m thinking about a metabox for page-like content types, to allow the user to choose an “Extra Content” or “Alternate content” option.

Routes to metabox
Routes to metabox

This solution would require the first point above to be addressed nonetheless but adding meta boxes to page and post edit screens is easier.

Next step

While in a quest to add archive pages to the menu I came across the Custom Post Type Archive Link plugin and had a look into the code. The plugin is compatible with WordPress 3.9 and sports code clean and easy to read. I will have a better look into it and will try to modify/copy it to suit my needs in case the first solution is the chosen one.

One thought on “Routing to callback functions in WordPress – 05

I appreciate your input