[wp-hackers] Page Title for Admin Menu

Chris Jean gaarai at gaarai.com
Fri Feb 13 21:36:43 GMT 2009


I don't believe that it's a bug; rather, I think that this is just a 
complex issue. Let me explain.

I dug through 1.5 - 2.7.1 and found that all of those versions have the 
page title parameter for top-level menu items. I also found that none of 
those versions actually define the page title for any of the menus built 
into core (they just use a blank string: '').

However, I have found that all of the versions actually do refer to the 
page title in the $menu data structure. You can find references to page 
title by searching for "$menu_array[3]" in the 
wp-admin/includes/plugin.php file (wp-admin/admin-functions.php file in 
versions before 2.3).

I traced the logic and found that its possible use comes down to the 
code found in the get_admin_page_title and get_admin_page_parent 
functions. Basically, if a parent can't be found (returned from 
get_admin_page_parent), there is the possibility that 
get_admin_page_title will use the top-level menu item's page title. 
However, when I looked through get_admin_page_parent, I can't think of 
any possible scenario where a parent wouldn't be found.

Looking farther back to version 1.2.2 (the only publicly available 
version I could find pre-1.5), I see that there is a single-level menu 
that doesn't support page titles. This combined with the fact that 1.5 
has two-level menus and more-or-less unused top-level menu page titles, 
I believe that I know why they are there.

I believe somewhere in 1.3 or 1.4 development, page titles were added to 
the menus. Then sometime before 1.5, second level menus were added which 
had their own page titles. These new page titles superseded the old page 
titles, but the top-level-menu page titles remained for backwards 
compatibility.

Here's where the story gets weird.

 From all the code I've looked through, the presence of a page title 
parameter for a top-level menu is nothing more than vestigial. Even in 
the earliest publicly available builds of WP where the parameter exists, 
the core doesn't make any use of the parameter and simply shuffles past 
its existence by filling in the field with empty strings.

Based on this, I would assume that there aren't any top-level menus that 
are devoid of sub-menu items and that the top-level menu items serve as 
nothing more than simple containers for the sub-menu items. And I would 
be wrong.

Just look at a new install of WordPress, and you will see a standalone 
top-level menu: Dashboard. In a standard install, Dashboard doesn't have 
any sub-menu items. When I realized this, I looked at the code and found 
that there weren't any entries in $submenu to create the Dashboard 
sub-menu entry that appears if plugins, such as Akismet, adds a sub-menu 
to the Dashboard top-level menu.

This confused me at first, but I looked at the add_submenu_page code and 
found that it generates the first $submenu entry automatically when a 
sub-menu page is added to a menu entry that doesn't have any sub-menu pages.

Even though the Dashboard menu entry doesn't have any sub-menu entries 
by default, it still does not make use of the page title parameter. 
Instead, it does something hackish (IMHO), it has to manually set the 
$title variable before get_admin_page_title is called. The 
get_admin_page_title will immediately return the value of the $title 
variable if it is not empty.

After seeing this, I cooked up the following quick plugin:

    <?php

    /*
    Plugin Name: Menu Test
    */

    add_action( 'admin_menu', menu_test_add_pages );

    function menu_test_add_pages() {
        $page_ref = add_menu_page( '', 'Menu Test', 10, 'menu-test',
    'menu_test_index' );
       
        add_action( 'load-' . $page_ref, 'menu_test_set_title' );
    }

    function menu_test_index() {
        echo "<h3>Menu Test</h3>";
    }

    function menu_test_set_title() {
        global $title;
        $title = 'Menu Test';
    }

    ?>

All this plugin does is register a menu entry that doesn't have any 
sub-menu entries and give the resulting page a title. This is more or 
less the same way that Dashboard has set the title since 1.5.

Sorry for the long reply. It was fun digging into this. I hope that my 
reply helps you out.

Chris Jean
http://gaarai.com/
http://wp-roadmap.com/
http://dnsyogi.com/



Chris Williams wrote:
> Exactly, so is that a bug?  Is it impossible/illegal to have a plug-in with
> only a top level menu?
>
>
>   
>> From: Gaarai <gaarai at gaarai.com>
>> Reply-To: <wp-hackers at lists.automattic.com>
>> Date: Thu, 12 Feb 2009 21:21:02 -0600
>> To: <wp-hackers at lists.automattic.com>
>> Subject: Re: [wp-hackers] Page Title for Admin Menu
>>
>> In my experience, it isn't the top-level menu that sets the title;
>> rather, the first sub-menu under it does.
>>
>> Chris Jean
>> http://gaarai.com/
>> http://wp-roadmap.com/
>>
>>
>>
>> Chris Williams wrote:
>>     
>>> Despite my overabundant use of the word "really" (which I "really" regret
>>> now :) ), does anybody have any input on this issue?
>>>
>>>
>>>   
>>>       
>>>> From: Chris Williams <chris at clwill.com>
>>>> Reply-To: <wp-hackers at lists.automattic.com>
>>>> Date: Wed, 11 Feb 2009 13:27:16 -0800
>>>> To: <wp-hackers at lists.automattic.com>
>>>> Subject: [wp-hackers] Page Title for Admin Menu
>>>>
>>>> I am writing a plugin that adds an admin menu page (not submenu).  It does
>>>> this because it¹s really quite a separate thing < a calendar of racing
>>>> events, nothing to do with the WP functions, it¹s really a separate data
>>>> table and everything.  So, it doesn¹t really belong down in ³settings², or
>>>> ³tools².
>>>>
>>>> This is all good, I have it working with add_menu_page and all is fine.  It
>>>> really only needs one page (like the ³Links² or ³Comments² menu items), it¹s
>>>> just an ³edit events² page.  There is one small problem, and that¹s that the
>>>> HTML page title is null.  It says simply ³[blogname] > -- Wordpress².
>>>> Nothing I put in the $page_title parameter on add_menu_page works.
>>>>
>>>> If I have a submenu page, it works, but not with just a plain add_menu_page.
>>>> I believe the issue is in get_admin_page_title in
>>>> wp-admin/includes/plugin.php, but I haven¹t debugged it completely.
>>>>
>>>> My questions are: 1) am I doing something incorrectly that is causing this,
>>>> 2) is this a known issue and I should just chill, or 3) should I figure it
>>>> out and propose a patch?
>>>>
>>>> Thanks,
>>>> Chris
>>>> _______________________________________________
>>>> wp-hackers mailing list
>>>> wp-hackers at lists.automattic.com
>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>>     
>>>>         
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>
>>>   
>>>       
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>     
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
>   


More information about the wp-hackers mailing list