WordPress.org

Ready to get started?Download WordPress

Forums

Theme-Check
[resolved] add_contextual_help deprecated - need help (8 posts)

  1. new_B
    Member
    Posted 1 year ago #

    Hi,

    Thanks for the useful plugin.

    I'm getting the following message:
    REQUIRED: add_contextual_help found in the file inc/theme-options.php. Deprecated since version 3.3. Use get_current_screen()->add_help_tab() instead.

    I've tried multiple variations but am having trouble getting it to work.

    Old code:
    add_contextual_help( $theme_page, $help );

    New code:

    get_current_screen()->add_help_tab( array(
       'id' => 'theme_options_help_tab',
       'title' => __('Theme Options Help Tab'),
       'content' => $help,
    ) );

    I'm getting the following error:

    Fatal error: Call to a member function add_help_tab() on a non-object

    Thanks in advance for any help you can provide.

    http://wordpress.org/extend/plugins/theme-check/

  2. new_B
    Member
    Posted 1 year ago #

    correction--noticed an extra comma

    New code:

    get_current_screen()->add_help_tab( array(
       'id' => 'theme_options_help_tab',
       'title' => __('Theme Options Help Tab'),
       'content' => $help
    ) );
  3. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 1 year ago #

    What action are you hooking this to?

    The way it works is that you create an admin page, then you can hook to it so that it will do stuff when that page is loaded. Like this:

    add_action('admin_menu', 'example_menu_add');
    function example_menu_add() {
    // add the menu item and screen
    $hook = add_menu_page( 'Whatever', 'Whatever', 'manage_options', 'whatever', 'whatever_admin' );
    add_action( "load-$hook", 'example_menu_load' );
    }

    In the function that is hooked to that load-$hook, I can call get_current_screen() and get back a valid object. But I can't just call get_current_screen from anywhere, because the screen object might not exist yet. The load-$hook is the hook for my specific screen, and it will get called to create the info for my screen, so then I can be assured that get_current_screen() will return something meaningful.

  4. new_B
    Member
    Posted 1 year ago #

    Thanks, Samuel. I have the following:

    /**
     * Add our theme options page to the admin menu, including some help
     * documentation.
     *
     * This function is attached to the admin_menu action hook.
     */
    function abc_theme_options_add_page() {
    
    $theme_page = add_theme_page(
    		__( 'Theme Options', 'abc' ),   // Name of page
    		__( 'Theme Options', 'abc' ),   // Label in menu
    		'edit_theme_options',    // Capability required
    		'theme_options',   // Menu slug, used to uniquely identify the page
    		'abc_theme_options_render_page' // Function that renders the options page
    	);
    
    	if ( ! $theme_page )
    		return;
    
    	$help = '<p>'.__('Some help text').'</p>';
    
    	 get_current_screen()->add_help_tab( array(
    		'id' => 'my_help_tab',
    		'title' => 'My Help Tab',
    		'content' => $help
    	 ));
    
    	//add_contextual_help( $theme_page, $help );
    }
    add_action( 'admin_menu', 'abc_theme_options_add_page' );
  5. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 1 year ago #

    You need to move the function calls that add the help page out into their own function with a different hook. Like this:

    function abc_theme_options_add_page() {
    
    $theme_page = add_theme_page(
    		__( 'Theme Options', 'abc' ),   // Name of page
    		__( 'Theme Options', 'abc' ),   // Label in menu
    		'edit_theme_options',    // Capability required
    		'theme_options',   // Menu slug, used to uniquely identify the page
    		'abc_theme_options_render_page' // Function that renders the options page
    	);
    
    	add_action( "load-$theme_page", 'abc_theme_options_add_help' );
    }
    add_action( 'admin_menu', 'abc_theme_options_add_page' );
    
    function abc_theme_options_add_help() {
    	$help = '<p>'.__('Some help text').'</p>';
    
    	 get_current_screen()->add_help_tab( array(
    		'id' => 'my_help_tab',
    		'title' => 'My Help Tab',
    		'content' => $help
    	 ));
    }
  6. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 1 year ago #

    Edit: The hook should be "load-$theme_page", with a dash, not an underscore like I typo'ed a moment ago.

  7. new_B
    Member
    Posted 1 year ago #

    Thanks so much Samuel--much appreciated. Worked like a charm!

  8. jmh
    Member
    Posted 1 year ago #

    Just used this to update a theme options page as well, thanks Otto!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.