I need to create a new menu and a sub menu item for the wordpress administrators. The menu is working fine but the sub menu items ( a duplicate sub menu for the main menu item is being created automatically.. I don't want that ). Both the sub menu items has their href attribute blank. I don't understand what the menu slug is. Please help..
// create custom plugin settings menu
add_action('admin_menu', 'retail_create_menu');
function retail_create_menu() { //create new top-level menu
add_menu_page('Retailers', 'Retailers', 'administrator', __FILE__, 'generate_retailer_list', 'http://localhost/apsm/wp-content/themes/wp-mediamag/functions/retail.ico');
add_submenu_page( __FILE__, 'Add Retailers', 'Add Retailers', 'administrator', 'add_ratilers.php', 'retailer_submenu_callback');
}
The file are inside a folder in my theme...
One simple hack to hide that duplicate menu:
add_submenu_page(
'__FILE__', // parent slug, same as main menu slug
'', // empty page title
'', // empty menu title
'administrator', // same capability as parent
'__FILE__', // same menu slug as parent slug
'generate_retailer_list', // same function as parent
)
Related
I'm currently developing my first plugin in WordPress. Since it's a admin only plugin I only need admin panel things.
I've started creating the plugins menu in the left panel:
I've did this by defining the main page and a submenu page:
add_menu_page( 'My Plugin', 'My Plugin', 'edit', 'my-plugin', null, 'none', '58' );
add_submenu_page( 'my-plugin', 'Settings','Settings', 'edit', 'my-plugin-settings', [
$this,
'settings_page'
] );
Now I have a submenu with 2 entries. So far so good but I want to change the default submenu page of my plugin to Dashboard instead of the plugins name. I can't find anything in the docs so maybe someone of you knows the trick.
You can't change the name of the main entry in the submenu - add_menu_page only lets you set the page title and the main menu title.
What you can do instead is add a new item to the submenu for the main page. Add a new menu item using add_submenu_page, and use the same slug you used in add_menu_page as both the $parent_slug and $menu_slug parameters, e.g.
$plugin_base_slug = 'my-plugin';
add_menu_page( 'My Plugin', 'My Plugin', 'edit', $plugin_base_slug, null, 'none', '58' );
/* Replace the main menu page in submenu with the menu item "Dashboard" */
add_submenu_page( $plugin_base_slug, 'Dashboard', 'Dashboard', 'edit', $plugin_base_slug, null);
/* Add the rest of your submenu pages */
add_submenu_page( $plugin_base_slug, 'Settings','Settings', 'edit', 'my-plugin-settings', [
$this,
'settings_page'
] );
This will replace the main page menu in the submenu (i.e. the one called My Plugin in your example) with the new one you create in add_submenu_page (called Dashboard in this example).
Just make sure you use the same capability and function in your new submenu item so that it does the same thing as the main menu page.
I have the following function which creates the menus in the admin area for my plugin.
function register_menu_items() {
add_menu_page(null, 'Ebay Motors', 'manage_options', 'ebay-motors-admin', array($this, 'ebay_motors_admin'));
add_submenu_page( 'ebay-motors-admin', 'Upload XML', 'Upload XML', 'manage_options', 'upload-xml', array($this, 'upload_xml'));
add_submenu_page( 'ebay-motors-admin', 'Archive XML', 'Archive XML', 'manage_options', 'archive_xml', array($this, 'archive_xml'));
}
This works, however (expectedly) the main menu item also creates a sub-menu item. Is there a way to create the main menu item without intern creating a submenu item. I'd also like the Main Menu item to link straight to the upload_xml function.
You can set the slug of your sub menu to be the same of the main menu item. This way you can overwrite the first menu item.
I'm adding a plugin menu (this gets added correctly) and a submenu under it (this never shows up) like this:
//We'll call the action to add the menu when the plugin is loaded
add_action( "plugins_loaded", "load" );
function load()
{
//Add us to the menu
add_action( "admin_menu", "addToMenu" );
}
function addToMenu()
{
//Main menu
add_plugins_page( "My Plugin", "My Plugin", "administrator", "my-plugin", "handlePlugin" );
//Sub Menu
add_submenu_page( "my-plugin", "test", "test", "administrator", "my-sub-slug", "handleSub" );
}
The above adds the "My Plugin" but not the "test" submenu. What am I doing wrong?
Wordpress does not allow 3rd level menu items by default. Code would be needed to alter how the Wordpress menu works to allow this. The workaround is to put your item not under plugins but at the top level.
I register a Custom Post Type, and I don't want it to have its own menu, instead I want to place it as a submenu of an existing admin menu item called my-custom-parent-page.
Here's my code:
register_post_type('my_custom_post_type',
array(
'labels' => array(
'name' => __('Books', 'mcpt'),
'singular_name' => __('Book', 'mcpt'),
),
'supports' => array('title', 'editor'),
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => 'my-custom-parent-page',
)
);
It works, meaning that it's properly located under the menu my-custom-parent-page, however now when I click on the parent menu (i.e. my-custom-parent-page) it points me to the my_custom_post_type page...
Any help?
Place a Custom-Post-Type in an submenu of an existing parent page
According to the Codex, this is a known and expected behavior:
Note: When using 'some string' to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top level link.
Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the "show_in_menu" section)
Here is the end of the quote which offers a solution:
If this isn't desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.
So this is quite simple to solve. However in my case I couldn't change the priority of the parent page because it is generated by a third-party library. Therefore I came up with this solution:
// Move the "example_cpt" Custom-Post-Type to be a submenu of the "example_parent_page_id" admin page.
add_action('admin_menu', 'fix_admin_menu_submenu', 11);
function fix_admin_menu_submenu() {
// Add "Example CPT" Custom-Post-Type as submenu of the "Example Parent Page" page
add_submenu_page('example_parent_page_id', 'Example CPT', 'Example CPT', 'edit_pages' , 'edit.php?post_type=example_cpt');
}
Please note the priority 11, and also when registering the Custom-Post-Type I set the "show_in_menu" parameter to false, so we can add it in the menu manually via add_submenu_page as shown above.
Properly set the Custom-Post-Type submenu entry as "active"
Now, the above solution works fine, however when creating/editing a post of the "example_cpt" Custom-Post-Type, it is not set as active and the submenu is not unfolded. Here is how to make sure that it is set as active, as well as the submenu in which it resides is properly set as active when creating/editing a post of the "example_cpt" Custom-Post-Type:
// Set the "example_parent_page_id" submenu as active/current when creating/editing a "example_cpt" post
add_filter('parent_file', 'fix_admin_parent_file');
function fix_admin_parent_file($parent_file){
global $submenu_file, $current_screen;
// Set correct active/current menu and submenu in the WordPress Admin menu for the "example_cpt" Add-New/Edit/List
if($current_screen->post_type == 'example_cpt') {
$submenu_file = 'edit.php?post_type=example_cpt';
$parent_file = 'example_parent_page_id';
}
return $parent_file;
}
Fine-tuning: Rename the first submenu entry
Furthermore, I also wanted the first menu entry of my submenu to be named differently from the parent name. By default, and using the code above, this is what we have:
- Example Parent Page
-- Example Parent Page
-- Example CPT
So as you can see, the first menu entry of the submenu is a duplicate of the parent menu, and this is the default WordPress behavior. I wanted to rename this duplicate entry to something different, much like WordPress does with the default menus (for example "Posts" and the submenu entry "All Posts" which both point to the same page but are named differently).
Here is how to rename the first submenu entry:
add_action('admin_menu', 'rename_first_submenu_entry', 11);
function rename_first_submenu_entry() {
// Rename first submenu entry (duplicate of parent menu) from "Example Parent Page" to "Submenu Text"
add_submenu_page('example_parent_page_id', 'Example Parent Page', 'Submenu Text', 'edit_pages' , 'example_parent_page_id');
}
Please note the priority 11, so it is renamed after it has been created. And now we have:
- Example Parent Page
-- Submenu Text
-- Example CPT
Please note that "Submenu Text" points to the same location as "Example Parent Page".
You also can simply set 'show_in_menu' in custom post type args to $menu_slug that you set in add_menu_page() that you want to set the CPT as sub menu of and set the priority of admin_menu function to 9 or lower. For example:
First, create a new top-level menu page, with priority set to 9 or lower (it's a must):
add_action( 'admin_menu', 'settings_menu' ), 9 );
function settings_menu() {
add_menu_page( __( 'Page Title' ), 'Menu Title', 'manage_options', 'menu_slug', show_page_callback() );
}
function show_page_callback() {
// show the settings page, plugin homepage, etc.
}
Then create custom post type with 'show_in_menu' arg set to menu_slug that we just set in settings_menu() function.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type('my_custom_post_type',
array(
'labels' => array(
'name' => __('Books', 'mcpt'),
'singular_name' => __('Book', 'mcpt'),
),
'supports' => array('title', 'editor'),
'public' => true,
'show_in_menu' => 'menu_slug',
);
}
Hope it helps.
can't say, what's exactly the reason, but it seems wordpress redirects to the first submenu-item.
So you have to create a new sub-menu-item with the same contents of your parent-menu-item.
add_action('admin_menu', 'my_admin_menu');
function my_admin_menu() {
global $submenu;
add_menu_page('My Menu', 'My Menu', 'administrator', 'my-menu', 'callback_func');
$parent = array('My Menu', 'administrator', 'my-menu', 'My Menu'); // new submenu-itm
$submenu['my-menu'] = fix_menu($submenu['my-menu'], $parent); // adds the new submenu-item at beginning of 'my-menu'-item
}
function fix_menu($submenu) {
array_unshift ($submenu, $parent);
return $submenu;
}
Hope it works for you.
Is it possible to add a new admin menu to the woocommerce admin section in Wordpress?
I've done this with WP E-commerce with my custom plugin so am wandering if the same is true for Woo commerce.
Thanks
Well, if you use something like this:
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'woocommerce', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
function my_custom_submenu_page_callback() {
echo '<h3>My Custom Submenu Page</h3>';
}
Then you will see a submenu under "Woocommerce" admin menu. For some reason you can´t do same using post_type=shop_order.
"shop_order" is the one you should use to put a submenu under "Woocommerce" one.. but, as i said, don´t know why didn´t work with that particual post_type.
http://codex.wordpress.org/Function_Reference/add_submenu_page
For me the following worked:
add_submenu_page(
'edit.php?post_type=product',
PAGE_TITLE,
MENU_TITLE,
'manage_woocommerce',
'custom_wc_menu'
);
Setting the $parent_slug to edit.php?post_type=product