Categories
Programming

WordPress – add_help_tab – add context help to custom admin plugin page

Add context tab help to custom admin plugin page using add_help_tab wordpress function. There is problem with example at wordpress.org api page if you are not using custom post type or custom option page. Solution for adding custom tab help option in you own custom admin plugin page.

How to add context help to custom admin plugin page? Some time ago I had to put help content to custom admin plugin page for my project.

context_help

There are pretty good examples at wordpress.org . Here is one of those examples :

<?php 
add_action('admin_menu', 'my_admin_add_page');

function my_admin_add_page() {
    $my_admin_page = add_options_page(__('My Admin Page', 'map'), __('My Admin Page', 'map'), 'manage_options', 'map', 'my_admin_page');

    // Adds my_help_tab when my_admin_page loads
    add_action('load-'.$my_admin_page, 'my_admin_add_help_tab');
}

function my_admin_add_help_tab () {
    $screen = get_current_screen();

    // Add my_help_tab if current screen is My Admin Page
    $screen->add_help_tab( array(
        'id'	=> 'my_help_tab',
        'title'	=> __('My Help Tab'),
        'content'	=> '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
    ) );
}
?>

This is working fine if you want to add context help to your custom option page within settings menu or to your custom post type. But if you have empty admin page then probably will nothing happened like in my case. Why is that?

I have search the web for solution but all examples have same basic code. There are always using custom post type or option page. I have no other option but to look at source of wordpress and see why that is not working in my case.

After some search through source code I found that there is missing context help structure in my custom admin plugin page.

Solution is to add it after adding help tabs (add_help_tab) with this function :

get_current_screen()->render_screen_meta();

Note : you need to call render_screen_meta before any other content of your admin page. For example if you have custom php page for your plugin admin page, this function needs to be called there first.

Fixed previus example :

<?php 
add_action('admin_menu', 'my_admin_add_page');
function my_admin_add_page() {
    // using add_submenu_page
    $my_admin_page = add_submenu_page('parent_admin_page', __('My Admin Page', 'map'), __('My Admin Page', 'map'), 'manage_options', 'map', 'my_admin_page');

    // Adds my_help_tab when my_admin_page loads
    add_action('load-'.$my_admin_page, 'my_admin_add_help_tab');
}

function my_admin_add_help_tab () {
    $screen = get_current_screen();

    // Add my_help_tab if current screen is My Admin Page
    $screen->add_help_tab( array(
        'id'	=> 'my_help_tab',
        'title'	=> __('My Help Tab'),
        'content'	=> '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
    ) );

    // render help context html structure
    // you can do that also at 'my_admin_page' callback function
    // added as last param in add_submenu_page function call
    $screen->render_screen_meta();
}

function my_admin_page {
    // or add here get_current_screen()->render_screen_meta();
    ...
    // other page content
}
?>

Note : this is also solution for adding custom option ( add_screen_option ).

2 replies on “WordPress – add_help_tab – add context help to custom admin plugin page”

Leave a Reply

Your email address will not be published. Required fields are marked *