$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Creating a WordPress admin page without a menu for a plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Creating a WordPress admin page without a menu for a plugin

matteradmin9PV0评论

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.

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.

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Feb 23, 2014 at 22:34 GreesoGreeso 2,2347 gold badges33 silver badges57 bronze badges 2
  • Have you seen this github/tommcfarlin/WordPress-Plugin-Boilerplate – Brad Dalton Commented Feb 23, 2014 at 23:55
  • @BradDalton: No I have not seen it before, however it looks interesting. I will take a look later on. – Greeso Commented Feb 24, 2014 at 5:14
Add a comment  | 

2 Answers 2

Reset to default 13

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:

  • http://codex.wordpress/Function_Reference/add_query_arg
  • http://codex.wordpress/Function_Reference/admin_url

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.

Post a comment

comment list (0)

  1. No comments so far