0

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.

5
  • What happens if you use...say 'my_test_string' instead of basename(__FILE__) for your slug?
    – mor7ifer
    Feb 14, 2012 at 14:15
  • 2
    I'm not sure if this can cause the problem, but you don't need basename() function, just use __FILE__.
    – Mamaduka
    Feb 14, 2012 at 14:22
  • m0r7if3r: I did the change and the generated link is broken too (point to a non existing page), is mysite.net/wp-admin/myplugin.php?page=my_test_string
    – leticia
    Feb 18, 2012 at 23:24
  • Mamaduka: Thank. I did the change and the problem disappears. In my local server works in both ways but in that installation only works if I put in the way you suggest. I really don't know why. If you put your reply as an answer I will select you.
    – leticia
    Feb 18, 2012 at 23:32
  • maybe was the fact that I change the menu slug so WordPress reload some information about the menu?
    – leticia
    Feb 18, 2012 at 23:39

1 Answer 1

3

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.

2
  • Thanks for the answer, but the questions indeed is why the second generated link is different than the first, because I want both be the same. Note that the second has myplugin.php?page=myplugin.php instead of admin.php?page=myplugin.php
    – leticia
    Feb 18, 2012 at 23:20
  • Thanks! I came here when I googled to see what the explanation of $menu_slug was trying to say. Very confusingly written in the codex so glad of the help.
    – Adam
    Jul 16, 2013 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.