[wp-hackers] Page Title for Admin Menu

Chris Jean gaarai at gaarai.com
Fri Feb 13 23:27:04 GMT 2009


It's true that Comments is like Dashboard. In fact, it is set up exactly 
the same way. If you look at the code, it doesn't have a page title 
declared for it either. Just like all the other top-level menu items of 
core, that entry is just an empty string.

Just like Dashboard, if you look in the wp-admin/comment.php file, 
you'll find it manually assigning the title to the $title variable.

If you are looking at the wp-admin/menu.php file and see where the array 
has the first entry defined, that's not the page title; its the menu 
title. If you look at the add_menu_page code, you see that the page 
title is put in the fourth array location while the menu title is put in 
the first array location.

I would agree with you on the idea of this being a bug if there is 
inconsistent code and things don't work properly. However, things do 
work properly and each instance of a stand-alone top-level menu item in 
core is coded the same way. This means that this odd situation is either 
a) a bad side-effect of trying to stay as backwards-compatible as 
possible or b) some very sloppy coding that was patched up quickly and 
never cleaned up.

Since I don't have access to all the code revisions between 1.2.2 and 
1.5, I can only make assumptions. However, the fact that the parameter 
exists, the manual entries into the $menu array all have a blank string 
in that array position, and the existence of the $title variable all 
suggest that this is due to supporting legacy code.

I may play around with core some more to see exactly what it takes to 
get_admin_page_title to return the page title of the top-level menu 
item. Maybe I can find a solution that is cleaner than the one that I've 
already provided. Who knows, I may even find a way to clean up core so 
that the page title of the top-level item is used in the event that it 
doesn't have any sub-menu items and $title isn't set. Of course, I'll 
have to do this without breaking anything.

Happy 0123456789 Day everyone.

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



Chris Williams wrote:
> Thank you for this comprehensive detective job, an excellent write-up, and a
> way around it.
>
> However, I'm even more convinced that this is a bug, not a feature.  There
> is another top menu item in core that has no submenus, and also has a proper
> page title, and that's Comments.  From what I can tell, it gets away with it
> because it manually loads the menu[] array, as other core items do.
>
> More to the point, however, I think it should be possible to create a
> plug-in that has only a top-menu item, and still benefits from a page title.
> Your ingenious solution below will be a temporary fix, but I think a real
> one is also in order.
>
> Thanks again for your detective work, perhaps you missed your calling -- the
> CIA could use help finding Bin Laden :)
>
> Chris
>
>
>   
>> From: Chris Jean <gaarai at gaarai.com>
>> Reply-To: <wp-hackers at lists.automattic.com>
>> Date: Fri, 13 Feb 2009 15:36:43 -0600
>> To: <wp-hackers at lists.automattic.com>
>> Subject: Re: [wp-hackers] Page Title for Admin Menu
>>
>> 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
>>>
>>>   
>>>       
>> _______________________________________________
>> 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