Positioning Screen (Contextual) Help Tabs

Using the new WP_Screen class makes it pretty easy to add help text to a screen.

<?php
add_action( "load-{$somepage}", 'wpse_load_reading' );
function wpse_load_reading()
{
    get_current_screen()->add_help_tab( array(
        'id'        => 'my-help-tab',
        'title'     => __( 'My Title' ),
        'content'   => __( 'Help Content' )
    ) );
}

This is great for custom pages. But when adding a help tab to an existing screen, let’s say options-reading.php, some weirdness happens.

Read More

The load-options-reading.php hook fires before the built in WP page adds its own help tabs. In other words, adding a help tab to an existing screen bumps all the built in help tabs to the bottom of the of list.

Here’s some code, if you’d like to try this out:

<?php
add_action( "load-options-reading.php", 'wpse_load_reading2' );
function wpse_load_reading2()
{
    get_current_screen()->add_help_tab( array(
        'id'        => 'my-help-tab',
        'title'     => __( 'My Title' ),
        'content'   => __( 'Why is this tab above the built in tab?' )
    ) );
}

Is there any way to reorder the help tabs on a screen?

EDIT:

Found a way around this. The default help tabs are added before the admin-header.php file is included.

So you can hook into load-{$built_in_page}, and from there hook a function admin_head which takes care of setting up your help tabs.

<?php
add_action( 'load-options-reading.php', 'wpse45210_load' );
function wpse45210_load()
{
    add_action( 'admin_head', 'wpse45210_add_help' );
}

function wpse45210_add_help()
{
    get_current_screen()->add_help_tab( array(
        'id'        => 'my-help-tab',
        'title'     => __( 'My Title' ),
        'content'   => __( 'This tab is below the built in tab.' )
    ) );
}

Seems kind of like a hack. Is there a better way?

Related posts

Leave a Reply

3 comments

  1. As @Mamaduka suggested, you can hook into admin_head-{$page_hook} and add the contextual help there. admin_head fires after the default contextual help tabs have been added.

    <?php
    add_action( 'admin_head-options-reading.php', 'wpse45210_add_help' );
    function wpse45210_add_help()
    {
        get_current_screen()->add_help_tab( array(
            'id'        => 'my-help-tab',
            'title'     => __( 'My Title' ),
            'content'   => __( 'This tab is below the built in tab.' )
        ) );
    }
    
  2. I guess you have three chances:

    1. Use WP_Screen->$_help_tabs to reorder them manually.
    2. Grap the existing help tabs, save them temporarily somewhere else. Then use WP_Screen->remove_help_tab( $id ) and then add them back in manually.
    3. Use the admin_head filter to populate the help tabs or missuse one of the filters or hooks that fire before it in admin-header.php