Route Pages – a way?

Setting up some tests was a question of time more than difficulties and I’m now facing the “hard” part of the task at hand: adding pages to WordPress backend “Pages” screen.
Before committing to some lengthy testing and coding I’ve took a peek to both the code and the database effects of WP Router to understand what’s happening “under the hood”.

Beginning from the end

Upon installation a single post, of the wp_router_page post type, will be created in the database and that will allow routes added at wp_router_generate_routes time to generate a page like the one below for the sample route in the Twenty Fourteen WordPress theme.
WP Router sample page
Using the Rewrite Rules Inspector plugin I know the


route is the first valid route answering that request. That’s the route created in the WP_Router_Sample_Class.php file: it all makes sense.
Rewrite Rules Inspector output dealing with WP Router sample route

The famous “Hello there” route

My objective is to create a page that, upon visiting, will redirect to a route. To test a possible approach I’ve added, in the WP_Router_Sample_Class.php file itself, a new /hello route accepting no parameters and simply displaying an “Hello there” message. Not a pinnacle of originality but I like to keep things simple.

class WP_Router_Sample { public static function init() { add_action('wp_router_generate_routes', array(get_class(), 'generate_routes'), 10, 1); } public static function generate_routes( WP_Router $router ) { $router->add_route('wp-router-sample', array( 'path' => '^wp_router/(.*?)$', 'query_vars' => array( 'sample_argument' => 1, ), 'page_callback' => array(get_class(), 'sample_callback'), 'page_arguments' => array('sample_argument'), 'access_callback' => TRUE, 'title' => 'WP Router Sample Page', )); // my `/hello` route $router->add_route('hello-route', array( 'path' => '^hello/?$', 'page_callback' => array(get_class(), 'name_callback'), 'access_callback' => TRUE, 'title' => 'Hello world route', )); } public static function sample_callback( $argument ) { echo '<p>Welcome to the WP Router sample page. You can find the code that generates this page in '.__FILE__.'</p>'; echo '<p>This page helpfully tells you the value of the <code>sample_argument</code> query variable: '.esc_html($argument).'</p>'; } public static function name_callback(){ echo "Hello there"; } } 

Browsing to my local server http://route-pages.local/hello will yield the expected result served by WP Router.
Hello route content display

The not so famous “Hello there” page

Moving forward in my investigation I’ve created a page, via the WordPress admin backend, called “Hello world route” and let WordPress go on and default a lot of its values
New Page screen adding the Hello world route page
and here it is in the posts table
Hello world route page line in posts table
Visiting the page will show the page content
Hello world route page content on the frontend
so nothing strange is happening here. Using Sequel PRO I’ve modified the page post_name value from hello-world-route to hello, saved, reloaded the page in the editing screen and visited it: here it is the “Hello there” message the route will return.
Sequel PRO: change the hello world route page post_name
Hello world route page post_name changed in the backend

That might be it

So that might be the road to the plugin objective: I will programmatically create a page for some routes with the proper post_name to have those pages behave like pages object, inheriting all the meta information pages can have, in the backend and like routes in the front-end.


Visiting the page poses no problems and will return the route callback function content, but trying to access some WordPress backend menus will either make the server run out of memory or the page be served blank. Since deactivating the WP Router plugin or restoring the original page post_name fixes it I have to infer it’s my fault. I will go over that next and then move to further steps.

I appreciate your input