{"id":213081,"date":"2011-05-23T08:00:00","date_gmt":"2011-05-23T08:00:00","guid":{"rendered":"https:\/\/www.weixiaoduo.com\/plugins\/2024\/02\/22\/wp-router\/"},"modified":"2012-08-06T23:06:00","modified_gmt":"2012-08-06T23:06:00","slug":"wp-router","status":"publish","type":"post","link":"https:\/\/www.weixiaoduo.com\/plugins\/wp-router\/","title":{"rendered":"WP Router"},"content":{"rendered":"<p>WordPress\u2019s rewrite rules and query variables provide a powerful system<br \/>\nfor mapping URL strings to collections of posts. Every request is parsed<br \/>\ninto query variables and turned into a SQL query via <code>$wp_query->query()<\/code>.<\/p>\n<p>Sometimes, though, you don\u2019t want to display a list of posts. You just want<br \/>\na URL to map to a callback function, with the output displayed in place of<br \/>\nposts in whatever theme you happen to be using.<\/p>\n<p>That\u2019s where WP Router comes in. It handles all the messy bits of registering<br \/>\npost types, query variables, rewrite rules, etc., and lets you write code to<br \/>\ndo what you want it to do. One function call is all it takes to map a<br \/>\nURL to your designated callback function and display the return value in the page.<\/p>\n<p>Created by <a href=\"http:\/\/flightless.us\" rel=\"nofollow ugc\">Flightless<\/a><\/p>\n<h3>Usage<\/h3>\n<h4>Creating Routes<\/h4>\n<ul>\n<li>Your plugin should hook into the <code>wp_router_generate_routes<\/code> action.<br \/>\nThe callback should take one argument, a <code>WP_Router<\/code> object.<\/li>\n<li>\n<p>Register a route and its callback using <code>WP_Router::add_route( $id, $args )<\/code><\/p>\n<ul>\n<li><code>$id<\/code> is a unique string your plugin should use to identify the route<\/li>\n<li>\n<p><code>$args<\/code> is an associative array, that sets the following properties for your route.<br \/>\nAny omitted argument will use the default value.<\/p>\n<ul>\n<li>\n<p><code>path<\/code> (required) \u2013 A regular expression to match against the request path.<br \/>\nThis corresponds to the array key you would use when creating rewrite rules for WordPress.<\/p>\n<\/li>\n<li>\n<p><code>query_vars<\/code> \u2013 An associative array, with the keys being query vars, and the<br \/>\nvalues being explicit strings or integers corresponding to matches in the path regexp.<br \/>\n Any query variables included here will be automatically registered.<\/p>\n<\/li>\n<li>\n<p><code>title<\/code> \u2013 The title of the page.<\/p>\n<\/li>\n<li>\n<p><code>title_callback<\/code> \u2013 A callback to use for dynamically generating the title.<br \/>\nDefaults to <code>__()<\/code>. If <code>NULL<\/code>, the <code>title<\/code> argument will be used as-is. if<br \/>\n    page_callback or <code>access_callback<\/code> returns <code>FALSE<\/code>, <code>title_callback<\/code> will not be called.<\/p>\n<pre><code>title_callback can be either a single callback function or an array specifying\n<\/code><\/pre>\n<p>callback functions for specific HTTP methods (e.g., <code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>, etc.).<br \/>\nIf the latter, the <code>default<\/code> key will be used if no other keys match the current<br \/>\nrequest method.<\/p>\n<\/li>\n<li>\n<p><code>title_arguments<\/code> \u2013 An array of query variables whose values will be passed<br \/>\nas arguments to <code>title_callback<\/code>. Defaults to the value of <code>title<\/code>. If an argument<br \/>\nis not a registered query variable, it will be passed as-is.<\/p>\n<\/li>\n<li>\n<p><code>page_callback<\/code> (required) \u2013 A callback to use for dynamically generating the<br \/>\ncontents of the page. The callback should either echo or return the contents of<br \/>\nthe page (if both, the returned value will be appended to the echoed value). If<br \/>\n    FALSE is returned, nothing will be output, and control of the page contents will<br \/>\nbe handed back to WordPress. The callback will be called during the <code>parse_request<\/code><br \/>\nphase of WordPress\u2019s page load. If <code>access_callback<\/code> returns <code>FALSE<\/code>, <code>page_callback<\/code><br \/>\nwill not be called.<\/p>\n<pre><code>page_callback can be either a single callback function or an array specifying\n<\/code><\/pre>\n<p>callback functions for specific HTTP methods (e.g., <code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>, etc.).<br \/>\nIf the latter, the <code>default<\/code> key will be used if no other keys match the current<br \/>\nrequest method.<\/p>\n<\/li>\n<li>\n<p><code>page_arguments<\/code> \u2013 An array of query variables whose values will be passed as<br \/>\narguments to <code>page_callback<\/code>. If an argument is not a registered query variable,<br \/>\nit will be passed as-is.<\/p>\n<\/li>\n<li>\n<p><code>access_callback<\/code> \u2013 A callback to determine if the user has permission to access<br \/>\nthis page. If <code>access_arguments<\/code> is provided, default is <code>current_user_can<\/code>, otherwise<br \/>\ndefault is <code>TRUE<\/code>. If the callback returns <code>FALSE<\/code>, anonymous users are redirected to<br \/>\nthe login page, authenticated users get a 403 error.<\/p>\n<pre><code>access_callback can be either a single callback function or an array specifying\n<\/code><\/pre>\n<p>callback functions for specific HTTP methods (e.g., <code>GET<\/code>, <code>POST<\/code>, <code>PUT<\/code>, <code>DELETE<\/code>, etc.).<br \/>\nIf the latter, the <code>default<\/code> key will be used if no other keys match the current<br \/>\nrequest method.<\/p>\n<\/li>\n<li>\n<p><code>access_arguments<\/code> \u2013 An array of query variables whose values will be passed<br \/>\nas arguments to <code>access_callback<\/code>. If an argument is not a registered query variable,<br \/>\nit will be passed as-is.<\/p>\n<\/li>\n<li>\n<p><code>template<\/code> \u2013 An array of templates that can be used to display the page. If a path<br \/>\nis absolute, it will be used as-is; relative paths allow for overrides by the theme.<br \/>\nThe string <code>$id<\/code> will be replaced with the ID of the route. If no template is found,<br \/>\nfallback templates are (in this order): <code>route-$id.php<\/code>, <code>route.php<\/code>, <code>page-$id.php<\/code>,<br \/>\n    page.php, <code>index.php<\/code>. If FALSE is given instead of an array, the page contents will<br \/>\nbe printed before calling <code>exit()<\/code> (you can also accomplish this by printing your output<br \/>\nand exiting directly from your callback function).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Example:<br \/>\n    $router->add_route(\u2018wp-router-sample\u2019, array(<br \/>\n        \u2018path\u2019 => \u2018^wp_router\/(.*?)$\u2019,<br \/>\n        \u2018query_vars\u2019 => array(<br \/>\n            \u2018sample_argument\u2019 => 1,<br \/>\n        ),<br \/>\n        \u2018page_callback\u2019 => array(get_class(), \u2018sample_callback\u2019),<br \/>\n        \u2018page_arguments\u2019 => array(\u2018sample_argument\u2019),<br \/>\n        \u2018access_callback\u2019 => TRUE,<br \/>\n        \u2018title\u2019 => \u2018WP Router Sample Page\u2019,<br \/>\n        \u2018template\u2019 => array(\u2018sample-page.php\u2019, dirname(<strong>FILE<\/strong>).DIRECTORY_SEPARATOR.\u2019sample-page.php\u2019)<br \/>\n    ));<\/p>\n<p>In this example, the path <code>http:\/\/example.com\/wp_router\/my_sample_path\/<\/code> will call<br \/>\nthe function <code>sample_callback<\/code> in the calling class. The value of the <code>sample_argument<\/code><br \/>\nquery variable, in this case \u201cmy_sample_path\u201d, will be provided as the first and only<br \/>\nargument to the callback function. If the file <code>sample-page.php<\/code> is found in the theme,<br \/>\nit will be used as the template, otherwise <code>sample-page.php<\/code> in your plugin directory will<br \/>\nbe used (if that\u2019s not found either, fall back to <code>route-wp-router-sample.php<\/code>, etc.).<\/p>\n<h4>Editing Routes<\/h4>\n<ul>\n<li>You can hook into the <code>wp_router_alter_routes<\/code> action to modify routes created by other plugins. The callback should take one argument, a <code>WP_Router<\/code> object.<\/li>\n<\/ul>\n<h4>Public API Functions<\/h4>\n<p>Creating or changing routes should always occur in the context of the <code>wp_router_generate_routes<\/code> or <code>wp_router_alter_routes<\/code> actions, using the <code>WP_Router<\/code> object supplied to your callback function.<\/p>\n<ul>\n<li><code>WP_Router::edit_route( string $id, array $changes )<\/code> \u2013 update each<br \/>\nproperty given in <code>$changes<\/code> for the route with the given ID. Any properties<br \/>\nnot given in <code>$changes<\/code> will be left unaltered.<\/li>\n<li><code>WP_Router::remove_route( string $id )<\/code> \u2013 delete the route with the given ID<\/li>\n<li><code>WP_Router::get_route( string $id )<\/code> \u2013 get the <code>WP_Route<\/code> object for the given ID<\/li>\n<li><code>WP_Router::get_url( string $id, array $arguments )<\/code> \u2013 get the URL to reach the route with the given ID, with the given query variables and their values<\/li>\n<li><code>WP_Route::get( string $property )<\/code> \u2013 get the value of the specified property for<br \/>\nthe <code>WP_Route<\/code> instance<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Provides a simple API for mapping requests to callback functions.<\/p>\n","protected":false},"author":65,"featured_media":228505,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WP Router - \u8587\u6653\u6735\u63d2\u4ef6\u76ee\u5f55","description":"Provides a simple API for mapping requests to callback functions."},"footnotes":""},"categories":[1],"tags":[51335,51336],"class_list":["post-213081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-callback-functions","tag-url-mapping"],"_links":{"self":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/213081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/comments?post=213081"}],"version-history":[{"count":0,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/213081\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media\/228505"}],"wp:attachment":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media?parent=213081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/categories?post=213081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/tags?post=213081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}