Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to add parameter for the submenus, but when I click to the admin page I receive the following message:

You do not have sufficient permissions to access this page.

Code:

add_submenu_page(  
'sandbox',                  
'Sandbox Options',          
'Options',                  
'administrator',            
'sandbox_options&tab=4',    
'sandbox_options_display'
); 

Without &tab=4 everything is okay.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

You'd have to manipulate the global $submenu and modify the link in it. Or use jQuery.

The following example adds a submenu in the Dashboard menu and changes the destination link just after. The submenu page will dump the contents of the global var.

add_action( 'admin_menu', function()
{
    add_submenu_page(  
        'index.php',                  
        'Sandbox Options',          
        'Options',                  
        'administrator',            
        'sandbox_options',    
        function() { global $submenu; var_dump($submenu); }
    );
    global $submenu; 
    $submenu['index.php'][11][2] = 'index.php?page=sandbox_options&tab=3';
});

[Update]

The example given, Redux Framework, uses the following technique:

  • Add a menu page with a slug example_slug.

  • Add the submenu pages and use the same slug example_slug + &tab=N.

  • All the menu and submenu pages are rendered with the menu callback. The submenu have null callbacks.

Example:

add_submenu_page(  
    'sandbox',                  
    'Sandbox Options',          
    'Options',                  
    'add_users',            
    'sandbox&tab=4',    
    '__return_null'
); 
share|improve this answer
    
Is it possible to allow url parameters without this 'hack'? I found this, and they did it in an other way: github.com/ghost1227/Redux-Framework/blob/master/options/… –  user1452062 Sep 19 '13 at 15:03
    
Answer updated with the Redux technique. –  brasofilo Sep 19 '13 at 15:40
add comment

Basically you need to reconstruct the url, because plugin_basename will strip your extra query args after you register the page with query args.

Full (working/tested) solution follows:

define( 'PARENT_SLUG', 'index.php' ); // where to put the submenu
define( 'PAGE_SLUG', 'wp190913_options' ); // submenu slug
define( 'EXTRA_ARG', 'tab' );
define( 'EXTRA_ARG_VALUE', 4 );

add_action( 'admin_menu', 'wp190913_add_page', 11 );
add_action( 'admin_menu', 'wp190913_add_page_args', 12 );

/**
 * Register submenu page
 */
function wp190913_add_page() {
    add_submenu_page(
        PARENT_SLUG,
        __('wp190913 Options', 'wp190913_textdomain' ),
        __('wp190913 Options', 'wp190913_textdomain'),
        'manage_options',
        PAGE_SLUG,
        'wp190913_display_page'
    );
}

/**
 * Add extra query arg for submenu page
 */
function wp190913_add_page_args() {
    global $submenu;

    $position = wp190913_search_submenu( PAGE_SLUG, PARENT_SLUG );

    // make sure we modify our page
    if ( is_int($position) && $submenu[PARENT_SLUG][$position][2] == PAGE_SLUG ) {
        // we will recompose the whole url, starting with parent
        $submenu[PARENT_SLUG][$position][2] = add_query_arg( 'page', PAGE_SLUG, PARENT_SLUG );
        $submenu[PARENT_SLUG][$position][2] = add_query_arg( EXTRA_ARG, EXTRA_ARG_VALUE, $submenu[PARENT_SLUG][$position][2] );
    }

}

/**
 * Find submenu key in it's parent array.
 *
 * @param string $page_slug
 * @param string $parent_slug
 *
 * @return null
 */
function wp190913_search_submenu( $page_slug, $parent_slug ) {
    global $submenu;

    if ( !isset( $submenu[$parent_slug] ) )
        return null;

    foreach ( $submenu[$parent_slug] as $i => $item ) {
        if ( $page_slug == $item[2] ) {
            return $i;
        }
    }

    return null;
}

/**
 * Submenu page content.
 */
function wp190913_display_page() {
    // Do stuff

    echo 'wp190913_display_page';
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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