5

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:

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.

2

2 Answers 2

12

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
  • Thanks for your reply. However, I need to create two separate pages. I will give you an example. In wordpress admin, if you click on POSTS, you will see a list of posts for your site. For each post, you can click on EDIT to view details about that particualr post. The details about the post, is only linked to via the list of posts. Also, postID is passed to this edit page, so it can display the correct post. I am doing the same thing but for a plugin, as descibed in my question.
    – Greeso
    Feb 23, 2014 at 23:06
  • Your solution was not 100% accurate, but it was close enough to inspire me to come up with the right solution. I will modify your answer to reflect the correct solution. Thanks.
    – Greeso
    Feb 24, 2014 at 4:53
1

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.

Your Answer

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

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