Saturday, January 7, 2023

WordPress - Add a custom menu item to the WordPress admin menu, for a user with administrator capability - Using WordPress Plugin development

You have to create a plugin first, say create a directory named "plugin1" under "wp-content/plugins" directory. Create a file named "plugin1.php" using following content:
<?php
/**
 * Plugin Name: plugin1
 * Plugin URI: https://www.your-site.com/
 * Description: Test.
 * Version: 1.0.1
 * Author: Plugin Owner Name
 * Author URI: https://www.your-site.com/
 **/

function register_my_custom_menu_page() {
    add_menu_page(
        __( 'Custom Menu Title', 'textdomain' ),
        'Custom Menu',
        'manage_options',
        'plugin1/home.php',
        '',
        plugins_url( 'plugin1/images/home2.png' ),
        6
    );
}
// This command will add menu at left bar of admin panel
add_action( 'admin_menu', 'register_my_custom_menu_page' );

// This will add stylesheet
wp_enqueue_style( 'plugin1.main.css', plugins_url( 'plugin1/main.css' ), false, '1.1', 'all');
Create an directory inside that directory "images" and put an image which will appear as menu icon in the admin menu panel, my structure is like as:
Go to "http://localhost/plugin-development/wp-admin/plugins.php" to activate your Plugin. Above code will add a left menu with icon to the menubar as below:
Once you click on the menu item, it will take you to the "plugin1/home.php" page which will looks like below:

No comments:

Post a Comment