avatar
action hook to build custom menu items WordPress

• We will use action hook named 'admin_menu' to add custom menu items. Here is an example:

add_action('admin_menu', 'location_plugin_render');

• The function add_action('admin_menu', 'location_plugin_render'); is used to register a callback function called location_plugin_render to be executed when the admin menu is being created.

function location_plugin_render() {

    // Menu Locations
    add_menu_page(
        __('Locations', 'manage-location'),      
        __('Locations', 'manage-location'),      
        'activate_plugins',                    
        'fl-location',                     
        '',
        'dashicons-admin-users',                 
        30
    );

    // Location Info
    add_submenu_page(
        'fl-appointment',
        'All Locations',
        'All Locations',
        'view_location_capability',
        'view-location',
        'location_info_render_page'
    );

    // New Appointment
    add_submenu_page(
        'fl-location',
        'Add New',
        'Add New',
        'add_location_capability',
        'add-location',
        'add_location_data_render_page'
    );

    // Edit Location
    add_submenu_page(
        'fl-location',
        'Edit Location',
        'Edit Location',
        'edit_location_capability',
        'edit-location',
        'edit_location_data_render_page'
    );

    remove_submenu_page('fl-location', 'fl-location');
}

• If you want to hide the Edit Location or Add New submenu items from the Locations menu, you can simply remove fl-location the corresponding add_submenu_page function calls from your code.

// New Appointment
add_submenu_page(
	'',
	'Add New',
	'',
	'add_location_capability',
	'add-location',
	'add_location_data_render_page'
);

// Edit Location
add_submenu_page(
	'',
	'Edit Location',
	'',
	'edit_location_capability',
	'edit-location',
	'edit_location_data_render_page'
);

» Slugs:

  • fl-location: This is the slug for the main menu item "Locations".
  • view-location: This is the slug for the submenu item "All Locations" under the "Locations" menu.
  • add-location: This is the slug for the submenu item "Add New" under the "Locations" menu.
  • edit-location: This is the slug for the submenu item "Edit Location" under the "Locations" menu.

» Capabilities:

  • activate_plugins: This capability is required to access the main menu item "Locations". It is commonly used for administrative-level access.
  • view_location_capability: This capability is required to access the submenu item "All Locations". It defines the capability needed to view the location information.
  • add_location_capability: This capability is required to access the submenu item "Add New". It defines the capability needed to add new locations.
  • edit_location_capability: This capability is required to access the submenu item "Edit Location". It defines the capability needed to edit existing locations.
You need to login to do this manipulation!