If you’ve been longing for gaining an additional control over your WordPress website’s admin bar, then you’ve landed on the right post. In this tutorial, I’ll be talking about the significance of adding a new custom menu item in the back-end along with the simple method of doing so the right way.
When does addition of a new custom menu item in WordPress admin serve handy?
The default WordPress admin dashboard comprises of some menus including Posts, Pages, Appearance, Plugins etc. which allow you to perform only the pre-defined functions. It is here that a custom menu item comes as a handy option. Unlike the hassles of installing a third-party plugin for doing a lot of other things with your menus; you can simply choose to create a custom menu item to accomplish your tasks within a remarkably short duration of time.
Now, let me make you familiar with the method of adding a new custom menu item in your WordPress back-end
Before proceeding ahead, let me make you acquainted with how the final outcome is going to be like. Well, the final product will be a custom menu item, linked to a page where you’ll be shown a refined list of users along with their roles. An ‘Edit’ link will be displayed against each user record, allowing you to modify the details for the respective user.
Before proceeding ahead with creation of a brand new menu item, you need to set the location for the same. For this, simply go to your admin dashboard-> Menus and select a preferred location for the secondary menu. Once you’re done with this, click on ‘Save’ button to save the settings.
The single-step method of adding custom menu item in WordPress admin
Simply open your WP theme’s functions.php file and add the below mentioned code to it:
<?php add_action( 'backend_menu', 'create_new_custom_menu_page' ); function create_new_custom_menu_page(){ add_custom_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'new_custom_menu_page','', 6 ); } function new_custom_menu_page() { $users = get_users(); ?> <style type="text/css"> table.usertable { font-family: calibri, verdana,arial; font-size:13px; color:#222222; border-width: 2px; border-color: #888888; border-collapse: collapse; } table.usertable th { background:#b3cfd1; border-width: 2px; padding: 6px; border-style: solid; border-color: #888888; } table.usertable td { background:#dcddc0; border-width: 2px; padding: 6px; border-style: solid; border-color: #888888; } </style> <h3>Custom Menu to display all users with their roles</h3> <table border="1" class="usertable"> <tr> <th>No.</th> <th>User Name.</th> <th>User Role.</th> <th>Action.</th> </tr> <?php $user_count=0; foreach($users as $user) { $user_count++; ?> <tr> <td><?php echo $user_count;></td> <td><?php echo $user->user_login; ?></td> <td><?php echo $user->roles[0]; ?></td> <td><a href="<?php echo get_edit_user_link($user->ID);?>">Edit</a></td> </tr> <?php } ?> </table> <?php } ?> |
A brief on what the above code is all about
1. add_action( ‘admin_menu’, ‘register_my_custom_menu_page’ );
This is the hook that registers the ‘create_new_custom_menu_page’ function under the backend_menu hook.
2. function create_new_custom_menu_page()
{
add_custom_menu_page( ‘custom menu title’, ‘custom menu’, ‘manage_options’, ‘custompage’, ‘my_custom_menu_page’,”, 6 );
}
This function will call another function ‘add_custom_menu_page’ which will add the menu into the installed WordPress theme. The seven parameters defined for add_custom_menu_page function are explained below:
- page title- this is the text that will be displayed along with the title tags of the custom menu page. When an admin user clicks on this custom menu, he/she will be able to view this text.
- Menu title- This will the title that appears for the custom menu within the website’s admin panel.
- Capability- This parameter will determine the capability that is required for displaying the custom menu to the user.
- Menu slug- This parameter will be used for managing the menu slug that will be displayed on the URL of the page once the menu has been clicked
- Function- Although this is an optional parameter, it displays the content stored for the custom menu page. In this tutorial, I’ve used the ‘new_custom_menu_page function which will generate the list of users, roles assigned to them along with an ‘Edit’ link for moderating the details of each user and his/her individual role.
- Menu Icon- This is yet another optional parameter which serves as the right tool for managing the icon defined for the custom menu. In this tutorial, I’m not passing any image path for the menu and hence WordPress would assign a default image to the menu’s icon.
- Menu Position- As the last(but optional) parameter, Menu Position enables you to manage the position of the menu. To put it simply, you can either choose to place the menu towards the top or the bottom; within the WordPress admin area.
With that you’re done with creating a new custom menu in your WordPress admin panel. The screen-shot for this new custom menu is shown below:
Final Thoughts
A custom menu renders you an absolute flexibility of managing your website in a much more refined manner as compared to how you’ve been doing it using the default WordPress admin menu. Working in synchronization with your specific requirements, a custom menu will serve as the right tool for embellishing your WordPress site/blog with brilliant add-ons.