Created
November 15, 2015 21:47
-
-
Save kadamwhite/6f1ac26f0081d2c599a1 to your computer and use it in GitHub Desktop.
WordPress routes with no backing content page: RFCodeReview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
THE GOAL: | |
Programmatically specify routes critical to the successful functioning | |
of the application, without any requirement for pages to be set up that | |
use specific templates or have specific names | |
THIS APPROACH: | |
We have validated that this approach works even if the user makes a page | |
slugged e.g. `add-survey`: the custom template has been named so that the | |
regular template hierarchy will pick the right thing. In all other cases | |
(where no page e.g. `add-survey` is available), WordPress will 404, but | |
will still load the right template. We suppress the 404 status code with | |
the `status_header` function. | |
In instances where an appropriately titled page IS available, it could | |
be possible to pull in that page's content so as to permit user-edited | |
copy to be flowed into the page. | |
THE QUESTION: | |
Is this a "good" way to do this? Are there any obvious gotchas here? | |
We suspect that not using rewrites for these routes probably means we are | |
incurring extra DB queries that we then throw away; that inefficiency is | |
"acceptable" given the side-effect of enabling later content management | |
as per the second approach paragraph, above. | |
*/ | |
/** | |
* Load specific templates when specific URLs are requested, overriding WP's | |
* internal page template hierarchy | |
* | |
* @param {String} $template The template that WP expects to load | |
* @return {String} The path to the template WP will actually load | |
*/ | |
function myproject_custom_routes( $template ) { | |
$request_uri = $_SERVER[ 'REQUEST_URI' ]; | |
function myproject_custom_title( $page ) { | |
// Expose the passed page title to the returned filter method via "use" | |
return function( $original_title, $sep, $seplocation ) use ( $page ) { | |
$site_title = get_bloginfo('name'); | |
return __( $page, 'myproject-efficiency' ) . " $sep $site_title"; | |
}; | |
} | |
foreach ( array( | |
'/survey/new' => array( | |
'title' => 'Add Survey', | |
'template' => 'page-add-survey.php' | |
), | |
'/surveys' => array( | |
'title' => 'Survey Reporting Dashboard', | |
'template' => 'page-dashboard-surveys.php' | |
), | |
'/timesheet/new' => array( | |
'title' => 'Add Time Sheet', | |
'template' => 'page-add-timesheet.php' | |
), | |
'/timesheets' => array( | |
'title' => 'Time Tracking Dashboard', | |
'template' => 'page-dashboard-timesheets.php' | |
) | |
) as $route=>$route_obj ) { | |
if ( $request_uri == $route || $request_uri == $route . '/' ) { | |
add_filter( 'wp_title', myproject_custom_title( $route_obj['title'] ), 10, 3 ); | |
status_header( 200 ); | |
return locate_template( array( $route_obj['template'] ) ); | |
} | |
} | |
return $template; | |
} | |
add_filter( 'template_include', 'myproject_custom_routes', 99 ); | |
function myproject_rewrite_rules() { | |
// Permit access to survey record "single" views by ID, not slug | |
add_rewrite_rule( | |
'^survey/([0-9]+)/?', | |
'index.php?post_type=myproject_survey&p=$matches[1]', | |
'top' | |
); | |
// Permit access to timesheet record "single" views by ID, not slug | |
add_rewrite_rule( | |
'^timesheet/([0-9]+)/?', | |
'index.php?post_type=myproject_timesheet&p=$matches[1]', | |
'top' | |
); | |
} | |
add_action( 'init', 'myproject_rewrite_rules' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment