WP Router
Provides a simple API for mapping requests to callback functions.
The facts
- Rating
- 3.7★ from 3
- Active installs
- 800+
- Price
- Free
- Last updated
- 6 Aug 2012
- Added
- May 2011
- Requires WP
- 3.0
- Tested up to
- WP 3.4.2
- Downloads
- 18,979
Our analysis
AI-assistedWP Router is a WordPress plugin that allows you to map URLs to custom callback functions instead of displaying a list of posts. It simplifies the process of registering post types, query variables, and rewrite rules, enabling you to create custom routes with minimal effort.
To use WP Router, you need to hook into the `wp_router_generate_routes` action and register your routes using the provided methods. This plugin is suitable for developers who want to create custom URL handling in their WordPress sites without dealing with the complexities of WordPress's built-in routing system.
Best for: This plugin is best for developers looking to implement custom routing in their WordPress projects.
What it does well
- ✓Simplifies URL mapping to callback functions
- ✓Handles registration of post types and query variables
- ✓Allows for custom page content generation
Where it falls short
- •Last updated in August 2012, may not be compatible with newer WordPress versions
- •Limited user ratings and active installs
Verdict
WP Router provides a straightforward way to manage custom URLs in WordPress, but its age and limited support may be a concern for some users.
From the developer
Jonathan Brinley's own description of WP Router, lightly tidied.
WordPress’s rewrite rules and query variables provide a powerful system
for mapping URL strings to collections of posts. Every request is parsed
into query variables and turned into a SQL query via $wp_query->query().
Sometimes, though, you don’t want to display a list of posts. You just want
a URL to map to a callback function, with the output displayed in place of
posts in whatever theme you happen to be using.
That’s where WP Router comes in. It handles all the messy bits of registering
post types, query variables, rewrite rules, etc., and lets you write code to
do what you want it to do. One function call is all it takes to map a
URL to your designated callback function and display the return value in the page.
Created by Flightless
Usage
Creating Routes
- Your plugin should hook into the
wp_router_generate_routesaction.
The callback should take one argument, aWP_Routerobject. -
Register a route and its callback using
WP_Router::add_route( $id, $args )$idis a unique string your plugin should use to identify the route-
$argsis an associative array, that sets the following properties for your route.
Any omitted argument will use the default value.-
path(required) – A regular expression to match against the request path.
This corresponds to the array key you would use when creating rewrite rules for WordPress. -
query_vars– An associative array, with the keys being query vars, and the
values being explicit strings or integers corresponding to matches in the path regexp.
Any query variables included here will be automatically registered. -
title– The title of the page. -
title_callback– A callback to use for dynamically generating the title.
Defaults to__(). IfNULL, thetitleargument will be used as-is. if
page_callback oraccess_callbackreturnsFALSE,title_callbackwill not be called.title_callback can be either a single callback function or an array specifyingcallback functions for specific HTTP methods (e.g.,
GET,POST,PUT,DELETE, etc.).
If the latter, thedefaultkey will be used if no other keys match the current
request method. -
title_arguments– An array of query variables whose values will be passed
as arguments totitle_callback. Defaults to the value oftitle. If an argument
is not a registered query variable, it will be passed as-is. -
page_callback(required) – A callback to use for dynamically generating the
contents of the page. The callback should either echo or return the contents of
the page (if both, the returned value will be appended to the echoed value). If
FALSE is returned, nothing will be output, and control of the page contents will
be handed back to WordPress. The callback will be called during theparse_request
phase of WordPress’s page load. Ifaccess_callbackreturnsFALSE,page_callback
will not be called.page_callback can be either a single callback function or an array specifyingcallback functions for specific HTTP methods (e.g.,
GET,POST,PUT,DELETE, etc.).
If the latter, thedefaultkey will be used if no other keys match the current
request method. -
page_arguments– An array of query variables whose values will be passed as
arguments topage_callback. If an argument is not a registered query variable,
it will be passed as-is. -
access_callback– A callback to determine if the user has permission to access
this page. Ifaccess_argumentsis provided, default iscurrent_user_can, otherwise
default isTRUE. If the callback returnsFALSE, anonymous users are redirected to
the login page, authenticated users get a 403 error.access_callback can be either a single callback function or an array specifyingcallback functions for specific HTTP methods (e.g.,
GET,POST,PUT,DELETE, etc.).
If the latter, thedefaultkey will be used if no other keys match the current
request method. -
access_arguments– An array of query variables whose values will be passed
as arguments toaccess_callback. If an argument is not a registered query variable,
it will be passed as-is. -
template– An array of templates that can be used to display the page. If a path
is absolute, it will be used as-is; relative paths allow for overrides by the theme.
The string$idwill be replaced with the ID of the route. If no template is found,
fallback templates are (in this order):route-$id.php,route.php,page-$id.php,
page.php,index.php. If FALSE is given instead of an array, the page contents will
be printed before callingexit()(you can also accomplish this by printing your output
and exiting directly from your callback function).
Example:
$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’,
‘template’ => array(‘sample-page.php’, dirname(FILE).DIRECTORY_SEPARATOR.’sample-page.php’)
));In this example, the path
http://example.com/wp_router/my_sample_path/will call
the functionsample_callbackin the calling class. The value of thesample_argument
query variable, in this case “my_sample_path”, will be provided as the first and only
argument to the callback function. If the filesample-page.phpis found in the theme,
it will be used as the template, otherwisesample-page.phpin your plugin directory will
be used (if that’s not found either, fall back toroute-wp-router-sample.php, etc.).Editing Routes
- You can hook into the
wp_router_alter_routesaction to modify routes created by other plugins. The callback should take one argument, aWP_Routerobject.
Public API Functions
Creating or changing routes should always occur in the context of the
wp_router_generate_routesorwp_router_alter_routesactions, using theWP_Routerobject supplied to your callback function.WP_Router::edit_route( string $id, array $changes )– update each
property given in$changesfor the route with the given ID. Any properties
not given in$changeswill be left unaltered.WP_Router::remove_route( string $id )– delete the route with the given IDWP_Router::get_route( string $id )– get theWP_Routeobject for the given IDWP_Router::get_url( string $id, array $arguments )– get the URL to reach the route with the given ID, with the given query variables and their valuesWP_Route::get( string $property )– get the value of the specified property for
theWP_Routeinstance
-