How do you create a custom admin menu for a plugin?

How do you create a custom admin menu for a plugin?

Minimal Steps to Create a Custom Admin Menu for a WordPress Plugin:

  1. Create Plugin Folder and File:

    • In the wp-content/plugins/ directory, create a folder (e.g., my-custom-menu-plugin).
    • Inside that folder, create a PHP file (e.g., my-custom-menu-plugin.php).
  2. Add Plugin Header: In the plugin file, add the header information:

Example

<?php
/**
 * Plugin Name: My Custom Admin Menu Plugin
 * Description: A plugin to add a custom admin menu.
 * Version: 1.0
 * Author: Your Name
 * License: GPL2
 */

Create the Custom Admin Menu: Use the add_menu_page() function to create a custom admin menu:

Example

<?php
function my_custom_menu_page() {
    add_menu_page(
        'Custom Plugin Page',        // Page title
        'Custom Menu',               // Menu title
        'manage_options',            // Capability
        'my-custom-menu-slug',       // Menu slug
        'my_custom_menu_page_html',  // Callback function
        'dashicons-admin-generic',   // Icon (optional)
        20                           // Position (optional)
    );
}
add_action('admin_menu', 'my_custom_menu_page');
?>

Create the Callback Function for the Page Content: Define the function that outputs the content on the custom menu page:

Example

<?php
function my_custom_menu_page_html() {
    if (!current_user_can('manage_options')) {
        return;
    }
    echo '<h1>Welcome to the Custom Admin Menu Page</h1>';
    echo '<p>This is your custom plugin admin page content.</p>';
}
?>

Activate the Plugin:

  • Go to the WordPress admin dashboard.
  • Navigate to Plugins > Installed Plugins.
  • Find your custom plugin and click Activate.

Related Questions & Topics

Powered and designed by igetvapeaustore.com | © 2024 codestap.com.