Menu bar

Tuesday 27 May 2014

How to add menu and submenu in wordpress wp-admin?

Administration Menus in Wordpress


Note: All this coding will be take place in functions.php of wordpress theme. I have already tested there. Its working smoothly.

To add an administration menu, you must know the wordpress functions to add menu and submenu:
add_menu_page();
syntax: <?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
add_action();
syntax: <?php add_action( $hook, $function_to_add, $priority,
$accepted_args ); ?>
add_submenu_page();
syntax: <?php add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); ?>

Three steps to follow:

1. Create a function.
2. Register the above function using the admin_menu action hook.
3. Create the HTML output for the page (screen) displayed when the menu item is clicked.


CREATE A FUNCTION:

<?php
function register_custom_menu_page() {
    add_menu_page('custom menu title', 'Confirm', 'add_users', 'custompage', '_custom_menu_page', null, 3);
}
?>

REGISTER THE ABOVE FUNCTION USING THE admin_menu ACTION HOOK:

<?php
add_action('admin_menu', 'register_custom_menu_page');
?>

CREATE THE HTML OUTPUT FOR THE PAGE (SCREEN) DISPLAYED WHEN THE MENU ITEM IS CLICKED:

<?php
function _custom_menu_page(){
   include 'foldername/Confirm.html';
}
?>

After this small effort you will see the admin menu was incremented by one more menu namely 'Confirm' and when you click on 'Confirm', output of Confirm.html will be displayed on the screen.

Now time to add submenu to this top level menu named 'Confirm'.
same procedure , but wordpress function changes.

CREATE A FUNCTION:

<?php
function register_my_custom_submenu_createcourse() {
add_submenu_page( 'custompage', 'Pending', 'Pending', 'manage_options', 'my-custom-submenu-createcourse', 'my_custom_submenu_createcourse_callback' );
}

?>

REGISTER THE ABOVE FUNCTION USING THE admin_menu ACTION HOOK:

<?php
add_action('admin_menu', 'register_my_custom_submenu_createcourse');
?>

CREATE THE HTML OUTPUT FOR THE PAGE (SCREEN) DISPLAYED WHEN THE MENU ITEM IS CLICKED:

<?php
function my_custom_submenu_createcourse_callback() {
include 'foldername/Pending.html';
}
?>
 That's all to add submenu to top level menu.
 If you like to add multiple submenu to top level menu then you need to change the function name foe each submenu and register admin_menu action hook accordingly.

<?php
 function register_custom_menu_page() {
   add_menu_page('custom menu title', 'Confirm', 'add_users', 'custompage', '_custom_menu_page', null, 3); 
}
add_action('admin_menu', 'register_custom_menu_page');

function _custom_menu_page(){
    include 'foldername/anypage.php';
}


add_action('admin_menu', 'register_my_custom_submenu_createcourse');
function register_my_custom_submenu_createcourse() {
add_submenu_page( 'custompage', 'Pending', 'Pending', 'manage_options', 'my-custom-submenu-createcourse', 'my_custom_submenu_createcourse_callback' ); 
}

function my_custom_submenu_createcourse_callback() {
  include 'foldername/anypage.php';
}


add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'custompage', 'Cancelation', 'Cancelation', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' ); 
}

function my_custom_submenu_page_callback() {
   include 'foldername/anypage.php';
}
?>

No comments: