• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
wpnzee logo

WPNZEE

  • Home
  • Trending
  • Terms
    • Privacy
    • Disclaimer
  • Support
  • Contact
You are here: Home / Topics / Understanding add_action() and add_menu_page() in WordPress Plugin Development

Understanding add_action() and add_menu_page() in WordPress Plugin Development

by

Home › Forums › WordPress › Flipnzee-Analytics plugin › Understanding add_action() and add_menu_page() in WordPress Plugin Development

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • May 16, 2026 at 12:42 pm #64
    Splendid
    Keymaster

    When developers build plugins for WordPress, they often need to create custom pages inside the WordPress admin dashboard. These pages can be used for analytics, settings, reports, SEO tools, AI dashboards, WooCommerce controls, and many other features.

    WordPress provides a powerful hook system that allows plugins to “attach” their functionality at different stages of WordPress execution. One common example is the admin_menu hook, which lets developers add new items to the admin sidebar menu.

    The following PHP code demonstrates how a plugin can create a custom admin dashboard page called Flipnzee Analytics.

    <?php
    
    add_action('admin_menu', function () {
    
        add_menu_page(
            'Flipnzee Analytics',
            'Flipnzee Analytics',
            'manage_options',
            'flipnzee-analytics',
            'flipnzee_admin_page',
            'dashicons-chart-area',
            25
        );
    
    });
    

    What This Code Does

    This code creates a new top-level menu item inside the WordPress admin dashboard.

    After activation, administrators would see something like:

    Dashboard
    Posts
    Media
    Pages
    Flipnzee Analytics
    

    When the user clicks Flipnzee Analytics, WordPress loads a custom admin page generated by the plugin.


    Breaking Down the Code Step by Step


    1. Starting PHP

    <?php
    

    This line tells the web server that PHP code begins here.

    Everything after this point is interpreted as PHP instructions.


    2. Using add_action()

    add_action('admin_menu', function () {
    

    WordPress uses a system called Hooks.

    Hooks allow developers to run their own code at specific moments during WordPress execution.

    The syntax is:

    add_action(hook_name, function_to_execute);
    

    In this case:

    'admin_menu'
    

    is the hook name.

    This means:

    “When WordPress is generating the admin menu, execute this function.”


    3. Anonymous Function

    function () {
    

    This is called an anonymous function.

    It is simply a function without a name.

    Instead of creating a separate named function like:

    function my_menu() {
    
    }
    

    the developer directly writes the function inside add_action().

    This is commonly done for short functions.


    4. Creating the Menu Page

    Inside the anonymous function:

    add_menu_page(
    

    This WordPress function creates a brand-new top-level admin menu page.

    It registers the menu item and tells WordPress how the page should behave.


    Understanding Each Parameter

    The add_menu_page() function accepts several arguments.


    Parameter 1 — Page Title

    'Flipnzee Analytics',
    

    This controls the title displayed at the top of the admin page and in the browser tab.

    For example:

    Flipnzee Analytics
    

    Parameter 2 — Menu Title

    'Flipnzee Analytics',
    

    This controls the text shown in the WordPress sidebar menu.

    So users will see:

    Flipnzee Analytics
    

    inside the admin menu.


    Parameter 3 — Capability

    'manage_options',
    

    This defines who is allowed to access the page.

    manage_options is usually reserved for Administrators.

    This means:

    • Admins can access the page
    • Normal users cannot

    WordPress automatically checks permissions before showing the page.


    WordPress Capabilities

    Capabilities are permission labels used internally by WordPress.

    Examples include:

    Capability Typical User Role
    manage_options Administrator
    edit_posts Author
    publish_posts Editor

    This permission system helps protect admin features.


    Parameter 4 — Menu Slug

    'flipnzee-analytics',
    

    This is the unique internal identifier for the page.

    WordPress uses it in the URL:

    /wp-admin/admin.php?page=flipnzee-analytics
    

    This slug helps WordPress identify which page should load.


    Parameter 5 — Callback Function

    'flipnzee_admin_page',
    

    This specifies the function responsible for displaying the page content.

    When the menu is clicked, WordPress runs:

    flipnzee_admin_page()
    

    The developer must create this function elsewhere in the plugin.

    Example:

    function flipnzee_admin_page() {
    
        echo '<h1>Flipnzee Analytics Dashboard</h1>';
    
    }
    

    Without this function, the page cannot display properly.


    Parameter 6 — Menu Icon

    'dashicons-chart-area',
    

    WordPress includes a built-in icon library called Dashicons.

    This parameter chooses the sidebar icon.

    The icon selected here represents charts or analytics.

    Other examples include:

    Dashicon Purpose
    dashicons-admin-users Users
    dashicons-store Ecommerce
    dashicons-media-code Code
    dashicons-chart-area Analytics

    Parameter 7 — Menu Position

    25
    

    This controls where the menu appears in the WordPress sidebar.

    Smaller numbers appear higher.

    Examples:

    Position Menu
    2 Dashboard
    5 Posts
    10 Media
    20 Pages

    A value of 25 places the menu slightly lower in the admin sidebar.


    What Happens Internally

    When the WordPress admin dashboard loads:

    1. WordPress fires the admin_menu hook
    2. The anonymous function runs
    3. add_menu_page() registers the custom page
    4. WordPress adds the menu item to the sidebar
    5. Clicking the menu executes:
    flipnzee_admin_page()
    

    to display the page content.


    Visual Workflow

    WordPress Admin Loads
            ↓
    admin_menu Hook Fires
            ↓
    Anonymous Function Executes
            ↓
    add_menu_page() Registers Menu
            ↓
    Menu Appears in Sidebar
            ↓
    User Clicks Menu
            ↓
    flipnzee_admin_page() Runs
    

    Expanded Beginner-Friendly Version

    The same functionality can also be written more explicitly:

    <?php
    
    function flipnzee_register_menu() {
    
        add_menu_page(
            'Flipnzee Analytics',
            'Flipnzee Analytics',
            'manage_options',
            'flipnzee-analytics',
            'flipnzee_admin_page',
            'dashicons-chart-area',
            25
        );
    
    }
    
    add_action('admin_menu', 'flipnzee_register_menu');
    

    This version may feel easier for beginners because the function has a name.


    Important WordPress Concepts Demonstrated

    This small code snippet introduces several important WordPress development concepts:

    • Hooks
    • Actions
    • Admin menu creation
    • Callback functions
    • User permissions
    • Plugin architecture
    • Dashboard customization

    These concepts form a major foundation of WordPress plugin development.


    Real-World Applications

    Developers use this exact approach to build:

    • SEO dashboards
    • Analytics systems
    • WooCommerce admin panels
    • LMS dashboards
    • Membership settings
    • AI-powered tools
    • Security plugins
    • Reporting systems

    Even many large commercial WordPress plugins internally use the same hook and menu architecture shown in this example.

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

Primary Sidebar

Recent Posts

    Archives

    No archives to show.

    Categories

    • No categories

    • Home
    • Trending
    • Terms
    • Support
    • Contact