Admin Hooks

Core Admin Hooks

  1. admin_init
    • Early hook for registering settings, capabilities, etc.
    • “The admin’s init hook”
  2. admin_menu
    • Add top-level/submenu pages
    • Pro Tip: Use add_menu_page() here
  3. admin_head-{hook_suffix}
    • Inject CSS/JS into specific admin pages
    • Example: admin_head-toplevel_page_my-plugin

🖥️ Page-Specific Hooks

  1. load-{hook_suffix}
    • Pre-render setup for specific pages
    • Example: load-edit.php (Posts list screen)
  2. admin_notices
    • Display admin notices/errors globally
    • Bonus: Use get_current_screen() to target specific screens
  3. in_admin_header
    • Insert content at the top of admin pages (before notices)

📝 Post/Editor Hooks

  1. save_post
    • Trigger actions when posts are saved
    • Advanced: Use $post->post_status checks
  2. add_meta_boxes
    • Register custom meta boxes for post types
  3. admin_enqueue_scripts
    • Enqueue scripts/styles ONLY in admin
    • Critical: Always check $hook_suffix

🗃️ List Table Hooks

  1. manage_{post_type}_posts_columns
    • Modify columns in post list tables
    • Example: manage_product_posts_columns
  2. manage_{post_type}_posts_custom_column
    • Render custom column content
    • Paired with the above hook
  3. bulk_actions-{screen}
    • Add/remove bulk actions
    • Example: bulk_actions-edit-post

⚙️ User & Dashboard Hooks

  1. show_user_profile / edit_user_profile**
    • Add fields to user profiles
  2. wp_dashboard_setup
    • Modify the WordPress dashboard
    • Example: Remove default widgets
  3. admin_bar_menu
    • Modify the admin toolbar
    • Pro Tip: Check is_admin() for context

🛠️ Advanced Power Hooks

  1. plugin_action_links_{plugin}
    • Add links to plugins list (e.g., “Settings”)
    • Example: plugin_action_links_my-plugin/my-plugin.php
  2. admin_post_{action}
    • Handle custom admin form submissions
    • Security: Always include nonce checks
  3. upgrader_process_complete
    • Run code after updates (plugins/themes/core)
  4. wp_ajax_{action} / wp_ajax_nopriv_{action}**
    • Handle AJAX requests (admin/front-end respectively)
  5. admin_xml_ns
    • Add XML namespaces to admin head (rare but useful for integrations)

🎯 When to Use Which Hook?

ScenarioRecommended Hook
Adding a settings pageadmin_menu
Saving post metasave_post
Modifying post columnsmanage_{type}_posts_columns
Adding help tabsload-{page}
Admin noticesadmin_notices
Custom admin AJAXwp_ajax_{action}

💡 Pro Tips

  • Debug hooks with:phpadd_action(‘all’, function($hook) { if (strpos($hook, ‘admin’) !== false) { error_log(“Admin hook fired: {$hook}”); } });
  • Always check capabilities:phpif (!current_user_can(‘manage_options’)) return;
  • Prefix your hooks to avoid collisions:phpadd_action(‘admin_init’, ‘myplugin_admin_init’);

add_action('all', function($hook) {
    if (strpos($hook, 'admin') !== false) {
        error_log("Admin hook fired: {$hook}");
    }
});
if (!current_user_can('manage_options')) return;
add_action('admin_init', 'myplugin_admin_init');