wp custom admin menu no plugin code example

Example: wp custom admin menu no plugin

// Check out the add_menu_page() WordPress documentation for more details:
// https://developer.wordpress.org/reference/functions/add_menu_page/


// 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 '';
    }
}

function myplugin_details_page_callback () {
    // This function is to display the hidden page (html and php)
}

Tags:

Misc Example