wordpress add_submenu_page adds broken link

I install my plugin in several sites and the submenu links generation at backed works fine, but fails in one specific site.

I use the follow code to add the submenu items:

add_menu_page(
    'Plugin name',
    'Plugin name',
    'manage_options',
    basename(__FILE__),
    'handle_this_action'
);

add_submenu_page(
    basename(__FILE__),
    'Link 1',
    'Link 1',
    'manage_options',
    basename(__FILE__),
    'handle_this_action'
);

This will generate a menu box at backed with one link inside and the main link in the title of menu box. When I click on the main link at menu box I get correctly redirected to my page with a URL like follow:

http://mysite.net/wp-admin/admin.php?page=myplugin.php

But when I click on the submenu link I’m wrongly redirected to:

http://mysite.net/wp-admin/myplugin.php?page=myplugin.php

Instead of: http://mysite.net/wp-admin/admin.php?page=myplugin.php

I really don’t know what I have to check in order to figure this out. The WordPress version used in the site that gives me this problem is Version 3.3.1 and I haven’t any other plugin activated.

How I can proceed?
Any tip or help will be appreciated.

Solutions Collecting From Web of "wordpress add_submenu_page adds broken link"

WordPress is doing exactly what you’re telling it to do. Refer to the Codex entry for add_submenu_page(). The functions arguments are like so:

<?php 
add_submenu_page( 
    $parent_slug, 
    $page_title, 
    $menu_title, 
    $capability, 
    $menu_slug, 
    $function 
);
?>

So, you’re assigning the same string to $parent_slug and $menu_slug. For obvious reasons, these two need to be different.

  • $parent_slug is the slug of the parent menu page. You assign that as basename( __FILE__ ) in your add_menu_page() call.
  • $menu_slug is the slug of the sub-menu page. Note what the Codex says:

    If you want to NOT duplicate the parent menu item, you need to set the name of the $menu_slug exactly the same as the parent slug.

While this statement is written confusingly, I think it’s saying that by setting $parent_slug and $menu_slug the same, you’re replacing the main-parent sub-menu entry with this page – and it sounds like that’s not what you want to do.