How to add an admin menu item to wordpress dashboard

When you create a wordpress plugin,sometimes you need to add an admin menu to wordpress dashboard.An admin menu makes your plugin more easy to access.Also with custom admin menu,you can show and add more features to your wordpress site.

In this article, we will introduce how to add an admin menu item to wordpress dashboard.

Step 1, Create an empty wordpress plugin. You can refer to this article How to create your first wordpress plugin

Step 2, Use a wordpress function to create and control the admin menu. You can use wordpress hook add_action() and add_menu_page() to add an admin menu to your wordpress admin dashboard.

add_action($tag, $function_to_add, $priority, $accepted_args)
add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position)

The snippets is as below

<?php
/**
 * Plugin Name: Your_Plugin
 * Plugin URI:  https://yourwebsite.com
 * Description: Your first wordpress plugin
 * Version:     1.0
 * Author:      WordPress Contributors
 * Author URI:  https://yourwebsite.com
 * Text Domain: your first wordpress plugin
 * Domain Path: /languages

*/

add_action('admin_menu', 'your_first_plugin');


function your_first_plugin(){
    add_menu_page('Yourfirstplugin', 'Your_first_plugin', 'manage_options', 'first_plugin', 'Your_Plugin_text');
    
}

function Your_Plugin_text(){?>
    <div class="wrap">
    <h1>your first plugin</h1>

    </div>
<?php
}

Step 3,Save the snippets as php document and upload it to wp-content/plugins. After activate it, you will see that a new admin menu appear in your wordpress admin dashboard.

However, when you enter the plugin by clicking the menu item, you will only see a title in this plugin, because it’s an empty plugin now. You can add your html code inside the last function to implement more features to your plugin.

Leave a Comment