If you are confused about how to add contextual help to your custom WordPress plugin, you can borrow my easy-to-use template.
Follow the steps below to add contextual help to your plugin.
Step 1: Copy the following code to a new file.
<?php
/**
* @author JasonLau.biz
* @copyright 2012
* @package Contextual Help Template
* @reference http://codex.wordpress.org/Function_Reference/get_current_screen
* @reference http://codex.wordpress.org/Plugin_API/Admin_Screen_Reference
* @example $screen = get_current_screen(); print $screen->ID; Result: {admin screen}_page_{plugin slug}
*/
if(!defined("ABSPATH")) die();
function my_help($contextual_help, $screen_id, $screen) {
$slug = ''; // Plugin slug
if($screen->id != '') // Plugin screen ID.
return;
$tabs = array(array(
__('Tab 1 Title'),
'<h2>' . __('Tab 1 Title', $slug) . '</h2>
<p>' . __('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse potenti. Quisque augue turpis, suscipit ac elementum id, facilisis at turpis. Nulla facilisi. Quisque congue iaculis nisi vel fringilla. Pellentesque elementum dapibus felis eget vestibulum. Vivamus hendrerit mi et massa tempor tristique eleifend tortor tempus. Morbi tempus consectetur erat, in hendrerit dolor bibendum ac. Ut porttitor lectus ac odio mattis eu ullamcorper nunc euismod. Fusce elementum sem feugiat nisl euismod eu luctus purus fermentum.', $slug) . '</p>'
), // Comma between tab arrays
array(
__('Tab 2 Title', $slug),
'<h2>' . __( 'Tab 2 Title', $slug) . '</h2>
<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc iaculis vehicula ornare. Duis luctus scelerisque bibendum. Integer pretium pellentesque nisl, quis rutrum ipsum hendrerit nec. Cras lorem est, commodo ut posuere ac, tincidunt ac massa. Sed tincidunt tempor enim id sagittis. Sed eleifend lectus ac tortor varius ut pharetra erat eleifend. Quisque tempor, nulla iaculis tincidunt accumsan, nunc dolor faucibus risus, at vestibulum mauris diam vel neque. Phasellus odio magna, lobortis id aliquet non, tincidunt at eros. Maecenas mollis, ligula nec tempus hendrerit, ipsum neque viverra odio, eget mattis tortor massa eget ante.', $slug) . '</p>'
) // No trailing comma
);
$x = 1;
foreach($tabs as $tab):
$screen->add_help_tab(array(
'id' => 'help_tab_' . $x,
'title' => __($tab[0]),
'content' => __($tab[1])
));
$x++;
endforeach;
}
add_filter('contextual_help', 'my_help', 10, 3);
?>
Step 2: Edit the code to suit your needs.
- Insert the slug and screen ID for your plugin at lines 15 and 16 of the above code.
$slug = 'PLUGIN SLUG GOES HERE'; // Plugin slug if($screen->id != 'SCREEN ID GOES HERE') // Plugin screen ID.
If you don't already know the screen ID, enter the following code in your plugin someplace and run it in the browser. The screen ID will be displayed wherever you place the code.
$screen = get_current_screen(); print $screen->ID;
- The contextual help tabs are divided into arrays. Each array has two parts - a title, and the tab content.
array( __('TAB TITLE GOES HERE', $slug), '<h2>' . __( 'TAB TITLE GOES HERE', $slug) . '</h2> <p>' . __( 'TAB CONTENT GOES HERE', $slug) . '</p>' )Add as many tabs as you wish by adding additional tab arrays, and adding a comma between each one. Be careful not to add a comma following the last tab array, or an error will occur.
Step 3: Save the file
Once you have completed adding tab arrays to the main tab array, save the file to your custom plugin's directory.
Step 4: Include the file.
Add the required "include" statement to your plugin. Be sure the path to the file is correct.
include(plugin_dir_path(__FILE__) . '/contextual-help.php');
When your plugin loads, the contextual help tab should appear in the upper-right of the screen, if all of your settings are correct.