[WordPress] 外掛分享: WP Router

前言介紹

  • 這款 WordPress 外掛「WP Router」是 2011-05-22 上架。
  • 目前有 1000 個安裝啟用數。
  • 上一次更新是 2012-08-06,距離現在已有 4655 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.0 以上版本才可以安裝。
  • 有 3 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

jbrinley |

外掛標籤

URL mapping | callback functions |

內容簡介

WordPress 的重寫規則和查詢變數提供了一個強大的系統,用於將 URL 字串映射到帖子集合。每個請求都會被解析成查詢變數,然後通過 $wp_query->query() 轉換為 SQL 查詢。

然而,有時您不想顯示帖子列表,只想將 URL 映射到回調函數中,並在您使用的任何主題中顯示輸出,這就是 WP Router 的作用。它處理註冊文章類型、查詢變數、重寫規則等混亂的部分,並讓您編寫代碼來實現您想要的功能,只需要一個函數調用即可使用 URL 將您指定的回調函數映射並在頁面中顯示返回值。

由 Flightless 創建

用法

創建路由

您的插件應掛鉤到 wp_router_generate_routes 動作。回調應該使用一個參數,即 WP_Router 對象。

使用 WP_Router::add_route( $id, $args ) 注冊路由及其回調

$id 是用於識別路由的唯一字符串。
$args 是一個關聯數組,為您的路由設置以下屬性。任何省略的參數都將使用默認值。

path (必需)- 與請求路徑匹配的正則表達式。這對應於在創建 WordPress 的重寫規則時您將使用的數組鍵。

query_vars - 一個關聯數組,其中鍵是查詢變數,值是與路徑正則表達式中匹配的明確字符串或整數。包括在此處的任何查詢變數都將自動註冊。

title - 頁面標題。

title_callback - 用於動態生成標題的回調。默認為__()。如果為 NULL,則將直接使用 title 參數。如果 page_callback 或 access_callback 返回 FALSE,將不調用 title_callback。

title_callback 可以是單個回調函數或指定特定 HTTP 方法的回調函數(例如 GET、POST、PUT、DELETE 等)的數組。如果是後者,則如果沒有其他鍵與當前請求方法匹配,則將使用 default 鍵。

title_arguments - 一個查詢變數的數組,其值將作為參數傳遞給 title_callback。默認為 title 的值。如果一個參數未註冊為查詢變數,它將按原樣傳遞。

page_callback (必需) - 用於動態生成頁面內容的回調。回調應該輸出或返回頁面的內容(如果兩者都有,則將返回的值添加到已輸出的值中)。如果返回 FALSE,將不輸出任何內容,並將控制權返回 WordPress,用於頁面內容。回調將在 WordPress 的頁面加載期間的parse_request階段調用。如果access_callback返回FALSE,將返回FALSE。

原文外掛簡介

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_routes action.
The callback should take one argument, a WP_Router object.

Register a route and its callback using WP_Router::add_route( $id, $args )

$id is a unique string your plugin should use to identify the route

$args is 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 __(). If NULL, the title argument will be used as-is. if
page_callback or access_callback returns FALSE, title_callback will not be called.
title_callback can be either a single callback function or an array specifying

callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.).
If the latter, the default key 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 to title_callback. Defaults to the value of title. 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 the parse_request
phase of WordPress’s page load. If access_callback returns FALSE, page_callback
will not be called.
page_callback can be either a single callback function or an array specifying

callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.).
If the latter, the default key 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 to page_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. If access_arguments is provided, default is current_user_can, otherwise
default is TRUE. If the callback returns FALSE, 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 specifying

callback functions for specific HTTP methods (e.g., GET, POST, PUT, DELETE, etc.).
If the latter, the default key 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 to access_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 $id will 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 calling exit() (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 function sample_callback in the calling class. The value of the sample_argument
query variable, in this case “my_sample_path”, will be provided as the first and only
argument to the callback function. If the file sample-page.php is found in the theme,
it will be used as the template, otherwise sample-page.php in your plugin directory will
be used (if that’s not found either, fall back to route-wp-router-sample.php, etc.).
Editing Routes

You can hook into the wp_router_alter_routes action to modify routes created by other plugins. The callback should take one argument, a WP_Router object.

Public API Functions
Creating or changing routes should always occur in the context of the wp_router_generate_routes or wp_router_alter_routes actions, using the WP_Router object supplied to your callback function.

WP_Router::edit_route( string $id, array $changes ) – update each
property given in $changes for the route with the given ID. Any properties
not given in $changes will be left unaltered.
WP_Router::remove_route( string $id ) – delete the route with the given ID
WP_Router::get_route( string $id ) – get the WP_Route object for the given ID
WP_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 values
WP_Route::get( string $property ) – get the value of the specified property for
the WP_Route instance

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「WP Router」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


最新版本

延伸相關外掛(你可能也想知道)

  • Custom URL Mapper 》總結:Custom URL Mapper是由Michael Kiamanesh和Grigor Nikolov製作和維護的完全免費外掛程式,可以讓您在WordPress網站上輕鬆創建自訂URL並將其映射到特定...。

文章
Filter
Apply Filters
Mastodon