
內容簡介
為 WordPress 網站的整個管理員區域加入小幫助功能的外掛程式。
使用方法
當您註冊標籤時,您可以有幾個不同的選項來決定該處提供哪些內容。
如果您在標籤參數中提供了回撥函數,它將優先於外掛程式尋找小幫助文件夾內的任何 HTML 檔案。
如果您在標籤參數中提供了文件,則外掛程式將專門尋找該文件,如果該文件不存在,則會輸出一條警告消息,以取代內容,通知開發人員該指定的小幫助文件不存在。
如果沒有傳遞檔案或回撥的參數到標籤中,則預設的外掛程式會尋找與標籤 ID 相同名稱的檔案。例如,對於名為“post-management”的標籤,它將查找小幫助文件夾內的“post-management.html”檔案。
HTML 小幫助文件夾
所有小幫助文件都應該存放在 get_template_directory() . '/includes/help-docs/'; 文件夾內,並且所有圖片都應該存放在 get_template_directory() . '/includes/help-docs/img/'; 文件夾內。
您可以使用 wp_contextual_help_docs_dir 過濾器來更改 HTML 檔案的目錄,並使用 wp_contextual_help_docs_url 過濾器來更改圖片的基本 URL。在您的小幫助文件中,我們使用變數 {WP_HELP_IMG_URL} 作為圖片 URL 的佔位符,然後在呈現之前插入從過濾器提供的值,或默認使用小幫助文件夾內的圖片。
註冊一個標籤
可以使用 WP_Contextual_Help::register_tab() 方法來註冊小幫助標籤。
參數
$id (string) – 將用作小幫助標籤的 ID 的字串
$title (string) – 在標籤內顯示給使用者的標題
$args (array) – 有關小幫助標籤的選項的一個選項陣列
page (string, array) – 啟用小幫助標籤的頁面
post_type (string, array) – 將標籤限制為僅顯示在這些特定文章類型上
file (string) – 要讀取並輸出到標籤內的 HTML 檔案
wpautop (boolean) – 預設:False - 對載入的 HTML 檔案應用 wpautop
callback - 如果用戶希望使用自訂回撥函數代替 HTML 檔案的自動載入,則可以在此處應用該函數
範例
<?php add_action( 'init', function(){ if( !class_exists( 'WP_Contextual_Help' ) ) return; // 只顯示在文章文章類型的“頁面(post.php)”和“新增文章(post-new.php)”上,並自動尋找名為“post-management.html”的檔案,該檔案位於 get_template_directory() . '/includes/help-docs/' 內 WP_Contextual_Help::register_tab( 'post-management', 'Post Management', array( 'page' => array( 'post.php', 'post-new.php' ), 'post_type' => 'post', 'wpautop' => true ) ); // 添加到自定義管理頁面 WP_Contextual_Help::register_tab( 'custom-settings', 'Custom Settings', array( 'page' => 'settings_page_custom-settings-page', 'wpautop' => true ) ); // 添加帶有自定義回撥函數的小幫助標籤 WP_Contextual_Help::register_tab( 'custom-callback', 'Custom Callback Example', array( 'page' => array( 'post.php', 'post-new.php' ), 'post_type' => 'post', 'callback' => function( $screen, $tab ) { echo '<p>It is super easy to add new help tabs!</p>'; } ) ); } ); ?>
外掛標籤
開發者團隊
原文外掛簡介
Adds helper functionality to easily add to the WP Contextual Help throughout the admin of a WordPress site.
Usage
When you register a tab you have a few different options as far as what content is displayed there.
If you provide a callback argument in the tab args, that will take precedence and the plugin will not look for any HTML files within the help-docs directory.
If you provide a file argument in the tab args, the plugin will look for that file specifically and if it does not exist will output a warning message in place of the content to notify the developer that the specified help document does not exist.
If no file or callback argument is passed into the tab args, by default the plugin will look for a file with the same name as the id of the tab. So for post-management it would look for post-management.html within the help docs directory.
HTML Help Docs
All help docs should either reside within the get_template_directory() . '/includes/help-docs/'; directory and all images within the get_template_directory() . '/includes/help-docs/img/';
You can use the wp_contextual_help_docs_dir filter to change the directory for the HTML files and the wp_contextual_help_docs_url filter to change the base URL for the images. Within your help documentation we use the variable {WP_HELP_IMG_URL} as a placeholder for the image URL which is then replaced before rendering with the value provided from the filter or defaults to the default help docs image directory.
Registering a tab
Help tabs are registered using the WP_Contextual_Help::register_tab() method.
Parameters
$id (string) – String to be used as the ID for the help tab
$title (string) – Title to display to the user within the tab
$args (array) – An array of options for the help tab
page (string, array) – Page(s) to enable the help tab
post_type (string, array) – Limit the tab to only display on these specific post types
file (string) – HTML file to read and output within the tab
wpautop (boolean) – Default: False – Apply wpautop to the loaded HTML file
callback – If a user would rather a custom callback instead of the autoloading of a HTML file, this is where that would be applied
Example
array( 'post.php', 'post-new.php' ), 'post_type' => 'post', 'wpautop' => true ) ); // Add to a custom admin page WP_Contextual_Help::register_tab( 'custom-settings', 'Custom Settings', array( 'page' => 'settings_page_custom-settings-page', 'wpautop' => true ) ); // Add help tab with custom callback WP_Contextual_Help::register_tab( 'custom-callback', 'Custom Callback Example', array( 'page' => array( 'post.php', 'post-new.php' ), 'post_type' => 'post', 'callback' => function( $screen, $tab ) { echo '
It is super easy to add new help tabs!
'; } ) ); } ); ?>
