Routing to callback functions in WordPress – 09

After creating a number of tools to test my code I’ve gone back to the original goal of creating a bridge between the WP Router plugin and the site administrator.

An example

I need to set up a page to show posts and events (a custom post type) relating to meetups. The page relative path would be /meetups.
Default workflow would be to

  1. create a page-meetup.php page template file in the theme
  2. create a “Meetups” page on the site

While the page-meetup.php template is under developer and version control the “Meetups” page has to be set up on every staging and production installation. If such pages come in a fair number then that is not a drama but I see the thing beginning to crumble at five.
A better workflow, decoupled from the slug naming, would be to use templates like

  1. create a meetups.php template file in the theme
  2. create a page on the site
  3. assign such page the “Meetups” template

this second solution might work better but would still need the administrator to create the required pages, assign each page the proper template and only point 1 would be under developer and version control.
Furthermore hard-coding relative paths in a page template would become risky as the possibility that the referred pages are missing or misnamed exists.

Philosophy of “not”

I like anything that allows me “not to do” or “not to care” about things; a simple example is an IDE auto-completion function: it allows me not to remember where a class or file is defined or located or not to have to look up a function or method signature.
The same way I think that if points 2 and 3 in the simple example above are required for the site to work properly than it should not be up to the administrator, a privileged user in the end, to implement them.

WP Router to the rescue

Mapping routes to templates or functions in code will allow a site developer to skip the points above completely having not to rely on exact page structure replication and to have the process under control while the administrator will not have to create pages and assign them templates.
The missing part is that now that “Meetups” page is nowhere to be found in the page list and has to be added to menus (a reasonable choice for such a page) using the “Links” menu and that’s another error-prone and administrator-domain step.

So create pages for routes

So after wrapping WP Router usage into Laravelesque sugar I’ve decided to try to fill that last gap of generating pages corresponding to routes to

  1. have them appear in the pages list
  2. give site administrators the possibility to assign a route-generated page meta information like the featured image

First iteration

To give my developing ambitions a finite limit I’ve written version 1.0 acceptance criteria and will code to those when working on the plugin

  • as a theme or plugin developer
    • I should be able to use the PersistableRoute and the Route classes via autoloading in my theme or plugin
    • I should see a page created for a route that should generate a page
    • I should not see a page created for a route that should not generate a page
    • I should be able to specify if a route page title will be editable by the user or not
    • I should be able to specify if a route page content will be editable by the user or not
  • as an administrator
    • I should not be allowed to activate the Route Pages plugin if WP Router is not installed
    • I should not be allowed to activate the Route Pages plugin if WP Router is not activated
    • I should not be allowed to activate the Route Pages plugin if a permalink structure is not set
    • I should see route pages in the pages list
    • I should be able to set a route page title if the route page title is set to be editable
    • I should not be able to set a route page title if the route page title is set not to be editable
    • I should be able to set a route page content if the route page content is set to be editable
    • I should not be able to set a route page content if the route page content is set not to be editable
    • I should not be able to set a route page permalink
    • I should be able to create route pages activating the plugin
    • I should be able to remove route pages deactivating the plugin
    • I should be able to selectively delete a route page from the page edit screen

The plugin is not safe for work at the moment but I will be releasing it on GitHub as soon as it becomes more stable.

I appreciate your input