How to create sub menu with a URL parameter?

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.

Read More

Code:

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

Without &tab=4 everything is okay.

Related posts

3 comments

  1. 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'
    ); 
    
  2. 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';
    }
    
  3. I solved it like this:

    class Banner{
        
        function __construct(){
            
            add_action('admin_menu', array($this,'adminmenu'), 200);
            
        }
        
        function adminmenu(){
            
            add_submenu_page( 
                                'adrotate', 
                                'AdRotate Pro · ' . __('Banner Tags', 'adrotate-pro'), 
                                __('Banner Tags', 'adrotate-pro'), 
                                'adrotate_ad_manage', 
                                'adrotate-banner-tags', 
                                array($this,'adrotate_banner_tags')
                        );
        }
        
        function adrotate_banner_tags(){
            
            wp_safe_redirect(admin_url('/edit-tags.php?taxonomy=' . TAX_TAGS));
            exit;
            
        }
    }
    new Banner();
    

Comments are closed.