Attach CPT to submenu
-
I’m currently developing a plugin that is using a Custom Post Type. I’d like to register my CPT and put it under another sub-menu. For example, main menu is STORE and the sub-menu is PRODUCTS.
I followed the guide lines from this blog entry
How to add a custom post type into the sub-menu of a custom menu
and I am still unable to add a new CPT, I get the following error message:You do not have sufficient permissions to access this page.
Everything works perfectly if I use CPT with his top-level menu otherwise I can only read, delete and edit CPTs but I can’t add new ones.
What is the correct way to attach the CPT into the submenu PRODUCTS?
Here my php codefunction register_cpt() { if ( is_user_logged_in() && current_user_can( 'edit_others_products' ) ) { $show = false; } else { $show = true; } $args = array ( 'public' => true, 'hierarchical' => false, 'has_archive' => true, 'show_ui' => true, 'show_in_menu' => $show, ); register_post_type( 'mf_product', $args ); } function setup_menu() { add_menu_page( ... ); // menu_store add_submenu_page( 'menu_store', 'Product Editor', 'Product Editor', 'edit_others_prodcuts', 'edit.php?post_type=mf_product', NULL ); }
-
I think you have a typo in the submenu capability: ‘edit_others_prodcuts’, you probably want ‘edit_others_products’.
With the typo, you shouldn’t even see the submenu to click on and get the insufficient permissions message, so something else might be going on here. You don’t show when you are calling
setup_menu()in your code. You may be calling too early. From theadd_submenu_page()docs:If you’re running into the “You do not have sufficient permissions to access this page.” message in a wp_die() screen, then you’ve hooked too early. The hook you should use is ‘admin_menu’.
Hi,
I’m callingsetup_menu()with the construct of a class using the init hook:index.php: add_action( 'init' 'admin_actions'); function admin_actions() { new Mf_Menu(); ... } class.mf-menu.php: class Mf_Menu() { public function __construct() { add_action( 'admin_menu', array ( $this, 'setup_menu') ); } ... }The menu is working fine when I set show_in_menu to true in
register_post_type(), but when its value is false or string post-new.php returns the error message and I can only edit/read/delete CPTs.
I have noticed that WordPress prints the message error when!user_can_access_admin_page()returns false in wp-admin/menu.php. So I looked for the condition that returns false in wp-admin/plugin.php it’sforeach (array_keys( $_wp_submenu_nopriv ) as $key ) if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) ) return false;when I set show_in_menu to false or string, the global variable $parent is set to an empty string. When I set show_in_menu to true then $parent is set to ‘edit.php?post_type=mf_product’ and everything works correctly but a top-level menu for the CPT is not what I want.
You’re using ‘init’ to instantiate your class object, you need to use ‘admin_menu’ in order to properly add a submenu page.
OK thank you.
The topic ‘Attach CPT to submenu’ is closed to new replies.