If you’ve used WordPress for long, you’ve probably noticed a contextual help tab in the upper-right hand corner of the dashboard. It looks something like this:


While working on the newest version of one of our plugins, I was looking for a place to display help information. After trying a few different solutions, I decided that I’d really like to use the same contextual help menu seen above. I was able to find instructions on Google, but they were spread over a few different websites, so I thought I’d put together this post. In this quick tutorial I’ll show you how you can add the contextual help menu to your plugin. It’s really a simple project.
First, I’m assuming that you’ve already created your plugin. This code can go anywhere in your plugin code, for my usage, I created a separate help.php and put all of this in it.
[php]
$screen = get_current_screen();
$screen->add_help_tab( array(
‘id’ => ‘my_help_tab’, // This should be unique for the screen.
‘title’ => ‘My Tab Title’,
‘content’ => ‘My Help Content’,
// ‘callback’ => ‘my_callback_function’,
// Use ‘callback’ instead of ‘content’ for a function callback that renders the tab content.
) );
[/php]
If you wanted to add multiple help tabs, you wouldn’t have to repeat $screen = get_current_screen();.
[php]
<pre>$screen = get_current_screen();
$screen->add_help_tab( array(
‘id’ => ‘my_help_tab’, // This should be unique for the screen.
‘title’ => ‘My Tab Title’,
‘content’ => ‘My Help Content’,
// ‘callback’ => ‘my_callback_function’,
// Use ‘callback’ instead of ‘content’ for a function callback that renders the tab content.
) );
$screen->add_help_tab( array(
‘id’ => ‘my_second_help_tab’, // This should be unique for the screen.
‘title’ => ‘My Second Tab Title’,
‘content’ => ‘More Help Tab Content’,
// ‘callback’ => ‘my_callback_function’,
// Use ‘callback’ instead of ‘content’ for a function callback that renders the tab content.
) );</pre>
[/php]
So, that’s all there is to it. In the examples above, I’ve set the content with the ‘content’ variable, but you can also use ‘callback’ to define a function which will output the help text. This is helpful if you want to create more dynamic help text.
On Wednesday we’ll talk about how to replicate the dashboard’s “Screen Options” tab in your plugins.
Leave a Reply