Performance: Admin menu pages
This affects the loading speed and responsiveness of WordPress admin dashboard pages, impacting how quickly menus and submenus appear and respond to user actions.
<?php add_action('admin_menu', function() { add_menu_page('Light Page', 'Light Page', 'manage_options', 'light-page', function() { echo '<div id="data-container">Loading data...</div>'; ?> <script> fetch(ajaxurl + '?action=load_data') .then(res => res.text()) .then(html => document.getElementById('data-container').innerHTML = html); </script> <?php }); }); add_action('wp_ajax_load_data', function() { global $wpdb; $results = $wpdb->get_results('SELECT * FROM wp_large_table LIMIT 50'); foreach ($results as $row) { echo esc_html($row->data) . '<br>'; } wp_die(); }); ?>
<?php add_action('admin_menu', function() { add_menu_page('Heavy Page', 'Heavy Page', 'manage_options', 'heavy-page', function() { global $wpdb; $results = $wpdb->get_results('SELECT * FROM wp_large_table'); foreach ($results as $row) { echo esc_html($row->data) . '<br>'; } }); }); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous data load in menu page | Large DOM tree generated at once | Multiple reflows triggered | High paint cost due to many elements | [X] Bad |
| Asynchronous data loading with AJAX | Small initial DOM, incremental updates | Single reflow after data load | Lower paint cost with fewer elements initially | [OK] Good |
add_menu_page function in WordPress?add_menu_page is used to create a new top-level menu in the WordPress admin dashboard.add_submenu_page, user roles are registered differently, and scripts/styles are enqueued with other functions.add_menu_page in WordPress?admin_menu action hook is the correct place to add admin menu pages.init runs too early, wp_head is for front-end, and add_filter is incorrect for this purpose.add_action('admin_menu', function() {
add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', function() {
echo 'Hello Admin!';
});
});add_action('admin_menu', 'register_my_menu');
function register_my_menu() {
add_menu_page('Sample', 'Sample', 'manage_options');
}add_menu_page is used correctly with all required parameters including slug 'custom-editor' and a callback function.Pass the test and this pattern moves unknown