Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP How to create new custom menu item in your wordpress back end?

How to create new custom menu item in your wordpress back end?

One question that I am asked about WordPress frequently is: how to add custom menu items to the WordPress admin menu? This is what we will tackle in this post.

The back end menu of your WordPress website may seem a little inflexible to you. Chances are that you might even urge for a custom menu, so as to have better control over your site’s admin bar.

In this post, we’ll help you understand the significance of custom menu, and how you can add it in your website admin menu.

Why You May Need to Add a Custom Menu?

WordPress website admin panel already has menus like Pages, Posts, Appearance etc. However, using the already existing menus make you perform pre-defined functions only. But, what if you want to do a lot of other things? For example, you may want to show a list of posts with their authors with a link to edit them, since it is not directly available in the WordPress admin panel, you may have to use a plugin or you can create a menu item to display that.

You can accomplish this goal by creating a custom menu, which will eventually help you have better control over your website admin area.

How to Create a Custom Menu in WordPress Admin?

When it comes to making tweaks to WordPress functionality, most of us (if not all) prefer following the easier route – use WordPress plugins. Most of the plugins will create the menu. However, you might not be able to find a plugin that can meet any other requirement like display a list of posts, their authors etc. And so, you will have to follow the process of creating the function as well as a menu item associated with it in your back end.

Following is one of the examples that will help you understand how you can create a Custom Menu in your back end. With this example, you will have a custom menu in the back end, and a page linked to it will show the list of users of your WordPress set up.

WordPress create new custom menu item in your wordpress back end

WordPress create new custom menu item in your wordpress back end

<?php
add_action( 'admin_menu', 'register_my_custom_menu_page' );
 
function register_my_custom_menu_page(){
    add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page','', 6 ); 
}
 
function my_custom_menu_page()
{
	$users = get_users();
?>
 
    <style type="text/css">
			table.usertable {
				font-family: verdana,arial,sans-serif;
				font-size:11px;
				color:#333333;
				border-width: 1px;
				border-color: #999999;
				border-collapse: collapse;
			}
 
			table.usertable th {
				background:#b5cfd2;
				border-width: 1px;
				padding: 8px;
				border-style: solid;
				border-color: #999999;
			}
 
			table.usertable td {
				background:#dcddc0;
				border-width: 1px;
				padding: 8px;
				border-style: solid;
				border-color: #999999;
			}
	</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
	}
?>

<?php add_action( 'admin_menu', 'register_my_custom_menu_page' ); function register_my_custom_menu_page(){ add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page','', 6 ); } function my_custom_menu_page() { $users = get_users(); ?> <style type="text/css"> table.usertable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.usertable th { background:#b5cfd2; border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } table.usertable td { background:#dcddc0; border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } </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 } ?>

After adding this code in your theme’s functions.php file, you can see a custom menu in your WordPress website admin panel (as shown in the screenshot below).

WordPress custom menu in your WordPress website admin panel

WordPress custom menu in your WordPress website admin panel

As you can see in the code above, a few lines of code will generate a menu and further code will assist you to insert whatever you want with the page associated with that menu.

If you’re just a beginner or any WordPress user without any programming knowledge, you will find the above code difficult to understand. So, to help you out let us provide a more detailed view of the code. We have used a simple code to add a menu to our WordPress admin menu sidebar. Let us now have a look on the first line of our code:

add_action( 'admin_menu', 'register_my_custom_menu_page' );

The add_action is a hook in WordPress programming that registers the ‘register_my_custom_menu_page’ function under the admin_menu hook. You can change the function name to something you like.

Note: Without making the add_action() call, an error will be thrown for undefined function when attempting to activate the custom menu.

Now let’s have a look at the register_my_custom_menu_page() function.

function register_my_custom_menu_page()
{
    add_menu_page( 'custom menu title', 'custom menu', 'manage_options', 'custompage', 'my_custom_menu_page','', 6 ); 
}

This function is used for calling a new function ‘add_menu_page’ that adds a menu in a WordPress theme. The function ‘add_menu_page’ contains 7 parameters and each of them have a certain meaning. So, let us have a look at those 7 parameters one by one:

  • 1) page title: the first parameter is for the page title or for the browser title. When someone clicks on our custom menu, the text to be displayed in the title tags of the page will be passed here.
  • 2) Menu title: the second parameter displays the Menu title which will appear in our site’s admin area.
  • 3) Capability: the third parameter determine the capability that is needed, in order to get the menu displayed to a user.
  • 4) Menu slug: This parameter is used to manage the menu slug which will appear on the URL of page, when the menu is clicked.
  • 5) Function (optional): It displays the content for the menu page. In our case, we have passed the ‘my_custom_menu_page’ function which helps in generating the user list, their role and profile edit link.
  • 6) Menu Icon (optional): Icons are the perfect way to communicate your message clearly like the way we do with words. And so, using the right icon for your custom admin menu is important. The sixth parameter is used to manage the menu icon. Here we need to pass an image path for our menu. Since, we are not passing any passing any value here, in that case, WordPress will assign a default icon to our menu.
  • 7) Menu Position (optional): Though even the last parameter is optional, but it is important, as it is used to manage the menu position (i.e. where you want your menu to appear, at the bottom or the top of the menu).

Wrapping It Up!

You can make the process of managing your WordPress website backend (admin panel) easier and quicker for yourself as well as your users, by adding a custom menu that gives you better authority over the default website admin menu.

In this post, you will learn about how to add a custom menu to display list of all WordPress users, their roles and link to edit their profiles. However, you can add your own custom menu as well to the menu that will work as per your own requirements.

Jan 12, 2015Ben Wilson
Calling Server.MapPath In C# ClassA handy guideline on adding custom menu item in WordPress admin
You Might Also Like:
  • A handy guideline on adding custom menu item in WordPress admin
  • Create Custom Update Profile Page For WordPress Users
  • Add And Get Custom Field In WordPress
  • Add WordPress Login Form On Sidebar Or Custom Page
  • WordPress Get All Custom Fields
  • How To Hide/ Remove / Disable WordPress Top Navigation Bar
  • Create A New Template For WordPress Page
  • Show Error Message On WordPress Custom Login Template Page
  • How To Change Default Home Page Of WordPress
  • Add More Extra Informations Or Fields To WordPress User Profile
Ben Wilson

Ben Wilson is a professional WordPress developer for a leading company WordPrax Ltd. He also provides the services like HTML to WordPress theme conversion and many more. If you are looking for the afforsaid services feel free to contact him.

6 years ago PHPadd_action, add_menu_page, admin_menu, Wordpress359
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,198 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,070 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,848 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,327 views
C# Read Json From URL And Parse/Deserialize Json
9,819 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development