內容簡介
此外掛可以註冊頁面鍵值,分配實際的 WordPress 頁面給它們,並透過各個獨立的鍵值存取這些頁面。
你是否曾經想要從樣板檔案內存取特定的頁面?當然,你可以透過標題來查詢。但是如果有人想要重新命名這個頁面呢?好吧,那我們可以選擇使用 slug。但是如果有人想要編輯 slug 以符合新標題呢?對,我知道,這是不好的想法。但是那個人可能不知道,或者根本不在乎。好吧,那麼讓我們使用頁面 ID 吧。噢,等等,現在那個人不小心永久刪除了那個頁面。在前端網站經過某個瀏覽者之後,那個人建立了一個新的頁面,標題和 slug 都和原始頁面完全相同。但是那個頁面仍舊不會顯示。
這就是 Page Keys 發揮作用的時候了。
用法
這個外掛的目的就是提供一種透過唯一而獨立的鍵值來存取特定頁面的方法。而這很簡單。在 Page Keys 管理頁面上,你可以加入任意多個獨特的頁面鍵值。對於每一個頁面鍵值,你可以選擇一個(已發佈的)頁面。點選「儲存變更」按鈕,就完成了。
假如你現在想要從你的佈景主題樣板檔案內存取一個特定的頁面。在定義了頁面鍵值、並分配了那個頁面後,你可以透過呼叫 get_page_by_key( $key ) 來取得相應的 WP_Post 物件。如果你已經在全域命名空間內定義了一個同名的函式,你必須自己設定這樣的函式。原則上,這只是其中一個可以在這個外掛的 functions.php 檔案內找到的複製品。
篩選器
為了自訂這個外掛的某些方面,它提供了幾個篩選器。下列是這些篩選器的簡短描述和有關如何修改預設行為的程式碼範例。只需將相應的程式碼片段放在你的佈景主題的 functions.php 檔案或自訂外掛內,或其它適當的地方即可。
edit_page_keys_capability
編輯頁面鍵值是受到限制的,預設的限制權限是 edit_published_pages。這樣設計的原因在於,使用者如果能夠編輯已發佈的頁面(例如,將其狀態更改為草稿),就隱含著他能夠破壞任何映射到該頁面的頁面鍵值。
/**
* 篩選需要編輯頁面鍵值的權限。
*
* @param string $capability 需要編輯頁面鍵值的權限。
*/
add_filter( 'edit_page_keys_capability', function() {
return 'manage_options';
} );
list_page_keys_capability
存取外掛的設定頁面也是受到限制的。為了區分那些只能看到現有頁面鍵值及其相應頁面的使用者和那些能夠編輯頁面鍵值的使用者,此處有兩種單獨的權限。預設意味著存取設定頁面需要的權限是 edit_pages。
/**
* 篩選需要列出頁面鍵值的權限。
*
* @param string $capability 需要列出頁面鍵值的權限。
*/
add_filter( 'list_page_keys_capability', function() {
return 'read';
} );
page_keys_show_admin_notice
根據你使用此外掛的方式,通知你有未指派頁面的頁面鍵值的管理提示可能比較惱人。令人高興的是,你可以透過篩選器來關閉此提醒。
add_filter( 'page_keys_show_admin_notice', '__return_false' );
外掛標籤
開發者團隊
原文外掛簡介
Register page keys, assign WordPress pages to them, and access each of these pages by its individual key.
Have you ever wanted to access a specific page from inside a template file? Of course, you could query it by its title. But what if someone wanted to rename the page? Okay, so we choose the slug. But maybe that someone also thought editing the slug as well to make it fit the new title was a very good idea. Yes, I know, it is not. But that someone either didn’t know, or didn’t care. Okay, so let’s use the page ID. Oh, wait, now that someone, by mistake, permanently deleted that page. After having visited the frontend that someone created a new page, with the exact same title and the exact same slug as the original page. But that damn page still won’t show.
This is exactly when Page Keys kicks in.
Usage
What this plugin is all about is providing a means to accessing specific pages by unique and therefore individual keys. And doing this is quite simple. On the Page Keys admin page, you can add as many unique page keys as you like. For each of these page keys, you then select a (published) page. Hit the Save Changes button, and you’re done already.
Suppose you now want to access a specific page in one of your theme’s template file. After having defined a page key and having assigned a page to it, you can get the according WP_Post object by calling get_page_by_key( $key ). In the rare case where you already have a function of that name defined within the global namespace, you would have to set up such a function by yourself. In principle, this is just a copy of what you can find in the functions.php file of this plugin.
Filters
In order to customize certain aspects of the plugin, it provides you with several filters. For each of these, a short description as well as a code example on how to alter the default behavior is given below. Just put the according code snippet in your theme’s functions.php file or your customization plugin, or to some other appropriate place.
edit_page_keys_capability
Editing the page keys is restricted to a certain capability, which is by default edit_published_pages. The reason for this choice lies with the fact that user, who is capable of editing a published page (e.g., changing its status to draft), implicitly is able to compromise any page key mapped to the page.
/**
* Filter the capability required to edit the page keys.
*
* @param string $capability Capability required to edit the page keys.
*/
add_filter( 'edit_page_keys_capability', function() {
return 'manage_options';
} );
list_page_keys_capability
Accessing the plugin’s settings page is restricted, too. In order to distinguish between users who are only allowed to see the existing page keys as well as their respective page, and users, who are able to edit page keys, there are two individual capabilities. The default for accessing the settings page is edit_pages.
/**
* Filter the capability required to list the page keys.
*
* @param string $capability Capability required to list the page keys.
*/
add_filter( 'list_page_keys_capability', function() {
return 'read';
} );
page_keys_show_admin_notice
Depending on how exactly you are working with the plugin, the admin notice informing you about page keys that don’t have a page assigned might be more annoying than helping. Gladly, there is a filter to turn this off.
add_filter( 'page_keys_show_admin_notice', '__return_false' );
Contribution
To contribute to this plugin, please see its GitHub repository.
If you have a feature request, or if you have developed the feature already, please feel free to use the Issues and/or Pull Requests section.
Of course, you can also provide me with translations if you would like to use the plugin in another not yet included language.
