Home >CMS Tutorial >WordPress >Up and Running with WordPress Contextual Help Screens
Leverage WordPress's Built-in Contextual Help for Enhanced Plugin/Theme Documentation
Developers often overlook comprehensive documentation for their WordPress plugins and themes. This article explores how to effectively utilize WordPress's built-in contextual help screens to improve user experience and provide readily available support. This approach offers a superior alternative to sparse plugin directory descriptions or unreliable external websites.
Key advantages of using contextual help:
Utilizing the WP_Screen
Class and get_current_screen()
Adding contextual help involves interacting with the WP_Screen
class via the get_current_screen()
function. This function identifies the current admin page, providing context for targeted help content.
Adding Help Tabs to All Admin Areas:
The following code demonstrates adding help tabs using both inline content and a callback function:
<code class="language-php">function add_context_menu_help(){ $current_screen = get_current_screen(); $content = '<p>This is a sample help tab.</p>'; $current_screen->add_help_tab( array( 'id' => 'my_plugin_basic_help', 'title' => __('Basic Help'), 'content' => $content )); $current_screen->add_help_tab( array( 'id' => 'my_plugin_advanced_help', 'title' => __('Advanced Help'), 'callback' => 'my_plugin_advanced_help_callback' )); } add_action('admin_head', 'add_context_menu_help'); function my_plugin_advanced_help_callback(){ echo '<p>More detailed help content here.</p>'; }</code>
Targeted Help Screens:
For more focused assistance, add help tabs only to specific admin pages. This example adds a help tab only to the edit screen of a custom post type "book":
<code class="language-php">function add_help_screen_to_books(){ $current_screen = get_current_screen(); if($current_screen->post_type == 'book' && $current_screen->base == 'edit'){ $content = '<p>Help specific to book post type.</p>'; $current_screen->add_help_tab( array( 'id' => 'my_plugin_book_help', 'title' => __('Book Help'), 'content' => $content )); } } add_action('admin_head', 'add_help_screen_to_books');</code>
Customizing the Contextual Help Sidebar:
The contextual help sidebar can be customized to display additional information. Remember to call set_help_sidebar
after adding help tabs:
<code class="language-php">function add_help_sidebar(){ $current_screen = get_current_screen(); if($current_screen->base == 'edit' || $current_screen->base == 'post'){ $current_screen->set_help_sidebar( '<p>Useful links and resources here.</p>' ); } } add_action('admin_head', 'add_help_sidebar', 11); // Higher priority to ensure it runs after tab additions.</code>
Conclusion:
By leveraging WordPress's built-in contextual help, developers can significantly enhance the user experience of their plugins and themes. This readily accessible, context-specific information empowers users and reduces reliance on external documentation. Remember to prioritize clear, concise, and relevant help content for optimal results.
The above is the detailed content of Up and Running with WordPress Contextual Help Screens. For more information, please follow other related articles on the PHP Chinese website!