Help Tabs with: add_help_tab() callback – How does the argument work?

Ok. The answer is NOT simple, but after some try and error, reading core, etc. I found out what the problem is:

The callback (which should be used instead of the content) accepts two arguments: $current_screen and $tab.

Here’s what $tab looks like, when dumped for a single tab.

Array
(
    Help Tabs with: add_help_tab() callback - How does the argument work? => TEST ME
    [id] => EXAMPLE_A
    [content] => 
    [callback] => Array
        (
            [0] => dmb_help Object
                (
                    [tabs] => Array
                        (
                            [EXAMPLE_A] => Array
                                (
                                    Help Tabs with: add_help_tab() callback - How does the argument work? => TEST ME
                                    [content] => FOO
                                )

                            [EXAMPLE_B] => Array
                                (
                                    Help Tabs with: add_help_tab() callback - How does the argument work? => TEST ME ALSO
                                    [content] => BAR
                                )

                        )

                )

            [1] => prepare
        )

)

IMPORTANT INFO: You’re not!! (never ever anyhow) allowed to use spaces inside an id-string. Then you can get the actual content from the object:

public function prepare( $screen, $tab )
{
    printf( 
         '<p>%s</p>'
        ,__( 
             $tab['callback'][0]->tabs[ $tab['id'] ]['content']
            ,'some_textdomain' 
         )
    );
}

You should drop content in the input array completely (until you don’t want to add some repeating content when looping through multiple help tabs).

Final working example:

Here’s the working text case as plugin.

<?php
/**
 * Plugin Name: Help Tab Test Case
 * Plugin URI:  http://unserkaiser.com
 * Description: Add Help Tab test case
 */
class example_help
{
    public $tabs = array(
        // The assoc key represents the ID
        // It is NOT allowed to contain spaces
         'EXAMPLE' => array(
             'title'   => 'TEST ME!'
            ,'content' => 'FOO'
         )
    );

    static public function init()
    {
        $class = __CLASS__ ;
        new $class;
    }

    public function __construct()
    {
        add_action( "load-{$GLOBALS['pagenow']}", array( $this, 'add_tabs' ), 20 );
    }

    public function add_tabs()
    {
        foreach ( $this->tabs as $id => $data )
        {
            get_current_screen()->add_help_tab( array(
                 'id'       => $id
                ,'title'    => __( $data['title'], 'some_textdomain' )
                // Use the content only if you want to add something
                // static on every help tab. Example: Another title inside the tab
                ,'content'  => '<p>Some stuff that stays above every help text</p>'
                ,'callback' => array( $this, 'prepare' )
            ) );
        }
    }

    public function prepare( $screen, $tab )
        {
            printf( 
             '<p>%s</p>'
            ,__( 
                     $tab['callback'][0]->tabs[ $tab['id'] ]['content']
                ,'dmb_textdomain' 
             )
        );
    }
}
// Always add help tabs during "load-{$GLOBALS['pagenow'}".
// There're some edge cases, as for example on reading options screen, your
// Help Tabs get loaded before the built in tabs. This seems to be a core error.
add_action( 'load-post.php', array( 'example_help', 'init' ) );
add_action( 'load-post-new.php', array( 'example_help', 'init' ) );

Leave a Comment