How to enable additional page in WordPress custom plugin?

I have a custom WordPress plugin that is showing me a list of data from database. I am registering it’s page via:

add_menu_page( 
    'Naročila', 
    'Vsa naročila', 
    'administrator', 
    'listaj-narocila', 
    array( &$this, 'listaj_narocila' )
);

And then of course I have function lista_narocila which is showing me my data.
So, currently my URL is:

Read More
http://domain.com/wp-admin/admin.php?page=listaj-narocila

And I show my data from database in a table. Now I have button DELETE and EDIT for each record, but I have a hard time figuring it out, how to register custom “url” or “custom page” inside WordPress that would allow me to have URL:

http://domain.com/wp-admin/admin.php?page=single-narocilo?id=X

I know I can try with add_menu_page but I don’t want this page to be in admin menus. Just to be available as URL. Currently I get no access error.

Related posts

Leave a Reply

1 comment

  1. You can create a sub menu page and pass null as its parent:

    $parent_slug
    Use NULL or set to ‘options.php’ if you want to create a page that doesn’t appear in any menu.

    A demo:

    add_action('admin_menu', function() 
    {
        # Main page
        add_menu_page( 
            'Vsa', 
            'Vsa', 
            'add_users', // Capability, not role
            'listaj-narocila', 
            function(){ 
                printf(
                    '<h2>%s</h2><a href="%s">%s</a>',
                    __( 'Main page' ),
                    admin_url( 'admin.php?page=single-norcilo&id='.rand(1,25) ),
                    __( 'Hidden sub page' )
                );
            },
            'http://sstatic.net/stackexchange/img/favicon.ico'
        );  
    
        # Child page    
        $hook = add_submenu_page(
            null,
            'Norcilo',
            'Norcilo',
            'add_users',
            'single-norcilo',
            function(){ 
                printf(
                    '<h2>%s</h2><a href="%s">%s</a>',
                    __( 'Hidden sub page' ),
                    admin_url( 'admin.php?page=listaj-narocila' ),
                    __( 'back' )
                );
            }
        );
    
        # Enqueue script in submenu page to fix the current menu indicator
        add_action( "admin_footer-$hook", function()
        {
            echo <<<HTML
    <script type="text/javascript">
    jQuery(document).ready( function($) {
        $('#toplevel_page_listaj-narocila')
            .removeClass('wp-not-current-submenu')
            .addClass('current');
    });     
    </script>
    HTML;
    
        });
    });
    

    An alternative approach: https://wordpress.stackexchange.com/a/114818/12615