Pawan Kumawat

Add Submenu In WooCommerce Product Menu In Admin

How to add a submenu item to the WooCommerce “Products” admin menu?

To add a custom menu in the WordPress admin panel, you can use the `add_menu_page()` or `add_submenu_page()` functions. Here’s a step-by-step guide on how to add a menu in the WordPress admin panel:

 

// Add a top-level menu page
function custom_menu_page() {
    add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position = null )

}
add_action('admin_menu', 'custom_menu_page');

// Callback function to render the menu page
function custom_menu_callback() {
    // Your menu page content goes here
    echo '<div class="wrap">';
    echo '<h1>Custom Menu</h1>';
    echo '<p>Welcome to the custom menu page!</p>';
    echo '</div>';
}

 

Parameter Description
$parent_slug
(string) (Required) The slug name for the parent menu (or the file name of a standard WordPress admin page).

$page_title
(string) (Required) The text to be displayed in the title tags of the page when the menu is selected.

$menu_title
(string) (Required) The text to be used for the menu.

$capability
(string) (Required) The capability required for this menu to be displayed to the user.

$menu_slug
(string) (Required) The slug name to refer to this menu by. Should be unique for this menu and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().

$function
(callable) (Optional) The function to be called to output the content for this page.
Default value: ”

$position
(int) (Optional) The position in the menu order this item should appear.
Default value: null

Documentation

So We must know the $parent_slug for adding submenu. for adding submenu to p-roduct menu in admin screen we must know the parent slug of product menu.

We can add a submenu item to the “WooCommerce” menu using “add_submenu_page” and ‘woocommerce’ for $parent_slug (using ‘admin_menu’ hook), but for Products menu ‘woocommerce’ $parent_slug can’t be used.

 

To add a submenu in the WooCommerce Product menu in the WordPress admin panel, you can use the `add_submenu_page()` function. Here’s an example of how you can achieve this:

1. Open the theme’s functions.php file in your WordPress theme directory (or create a new plugin file if you prefer).
2. Add the following code to the file:

// Add submenu in WooCommerce Product menu
function add_custom_submenu() {
    add_submenu_page(
        'edit.php?post_type=product', // Parent menu slug
        'Custom Submenu',             // Page title
        'Custom Submenu',             // Menu title
        'manage_woocommerce',         // Required capability to access the menu page
        'custom-submenu',             // Menu slug
        'custom_submenu_callback'     // Callback function to render the submenu page
    );
}
add_action('admin_menu', 'add_custom_submenu');

// Callback function to render the submenu page
function custom_submenu_callback() {
    // Your submenu page content goes here
    echo '<div class="wrap">';
    echo '<h1>Custom Submenu</h1>';
    echo '<p>Welcome to the custom submenu page!</p>';
    echo '</div>';
}

 

3. Save the changes and upload the modified functions.php file (or the plugin file) back to your server.

This code adds a submenu page with the title “Custom Submenu” in the WooCommerce Product menu in the WordPress admin panel. The `add_submenu_page()` function accepts several parameters, including the parent menu slug (in this case, `’edit.php?post_type=product’`), page title, menu title, required capability (in this case, `’manage_woocommerce’`), menu slug, and callback function to render the submenu page.

The `custom_submenu_callback()` function is the callback function that renders the content of the submenu page. You can modify this function and add your own HTML and PHP code to customize the content.

After making these changes, you should see a new submenu item labeled “Custom Submenu” in the WooCommerce Product menu within the WordPress admin panel. Clicking on the submenu item will display the content defined in the `custom_submenu_callback()` function.

Categories

Related Blogs

How To Limit Search To Only Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?