Creating a WordPress admin page without a menu for a plugin

I am writing a plugin, that will list a number of entries from a custom set of tables. I added the main page for the plugin using the following WordPress functions:

// Add menu and pages to WordPress admin area
add_action('admin_menu', 'myplugin_create_top_level_menu');

function myplugin_create_top_level_menu() {
    add_menu_page('MyPlugin', 'MyPlugin', 'manage_options', 'myplugin-top-level-admin-menu');
    add_submenu_page('myplugin-top-level-admin-menu', 'MyPlugin Admin Page', 'Admin Page', 'manage_options', 'myplugin-top-level-admin-menu', 'myplugin_admin_page');
}

function myplugin_admin_page {
    // Code to display the admin page for my plugin (both php and html code)
    // This includes the following seudo code (in php)
    foreach ($results_from_db as $result) {
        // CODE TO DISPLAY RESULTS IN AN HTML TABLE *** I NEED HELP HERE ***
    }
}

Now, if you read the above code closely, you note that there is a comment that states ‘I NEED HELP HERE’; here are more details:

Read More

I know how to display everything on the the admin page that I created. The admin page will read from the custom tables, and display results as HTML table rows.

I only need to link each row to a page, lets call it ‘Entry Details Page’. The idea is, for each row in the HTML table, there will be a link, and when I click on that link, it will take me to another page that displays more details about that row.

I was thinking of using add_submenu_page as described here, but honestly I did not understand how to use it and how to include it in my code. I tried something like this but I think it is wrong:

function myplugin_admin_page {
    // Code to display the admin page for my plugin (both php and html code)
    // This includes the following seudo code (in php)
    foreach ($results_from_db as $result) {
        // CODE TO DISPLAY RESULTS IN AN HTML TABLE *** I NEED HELP HERE ***

        // The following line of code is incorrect, but to show you the idea
        echo '<a href="' . add_submenu_page(NULL,'Entry Details Page','Entry Details Page','manage_options','details-page', 'myplugin_details_page'); . '">View</a>';

    }
}

myplugin_details_page () {
    // Code to display the details page
}

Now, the two problems I am facing are:

  1. How to add the details page correctly (it is clear that what I am doing above in the second code snippet is incorrect)?
  2. How to include params in the details page (I need to pass a row id to view the details)?

I think I am really close to solving the problem, however I could not find enough documentation to finish it, so please help, and I really really thank you.

Cheers.

Related posts

2 comments

  1. I am less convinced that I know what you are doing than I once was.

    // Add menu and pages to WordPress admin area
    add_action('admin_menu', 'myplugin_create_top_level_menu');
    
    function myplugin_create_top_level_menu() {
    
        // This is the menu on the side
        add_menu_page(
          'MyPlugin', 
          'MyPlugin', 
          'manage_options', 
          'myplugin-top-level-page'
        );
    
        // This is the first page that is displayed when the menu is clicked
        add_submenu_page(
          'myplugin-top-level-page', 
          'MyPlugin Top Level Page',
          'MyPlugin Top Level Page', 
          'manage_options', 
          'myplugin-top-level-page', 
          'myplugin_top_level_page_callback'
         );
    
         // This is the hidden page
         add_submenu_page(
          null, 
          'MyPlugin Details Page',
          'MyPlugin Details Page', 
          'manage_options', 
          'myplugin-details-page', 
          'myplugin_details_page_callback'
         );
    }
    
    function myplugin_top_level_page_callback() {
    
        global $wpdb;
        $results_from_db = $wpdb->get_results("SELECT * FROM myplugin_custom_table");
    
        foreach ($results_from_db as $result) {
    
            $id = $result->id;
    
            $link = add_query_arg(
                array(
                    'page' => 'myplugin-details-page', // as defined in the hidden page
                    'id' => $id
                ),
                admin_url('admin.php')
            );
    
            echo '<ul>';
            echo '<li><a href="'.$link.'">'.$id.'</a><li>';
            echo '</ul>';
        }
    }
    
    function myplugin_details_page_callback () {
        // This function is to display the hidden page (html and php)
    }
    

    You are using two additional Core functions in there, so for reference:

  2. It’s better to add another parameter in URL and call it “action” and based on action render a different template for different action.

    Benefit: It will keep the menu open (active). In the above solution, it will
    not keep the menu open for a hidden menu as we’re technically removing
    parent of a submenu.

Comments are closed.