Add CPT submenu to profile
-
Hello, I’m trying to add a submenu to the subscriber profile with his Content Post Type with a new role that has the available permission to “Add New”. I’m passing the following args to register_post_type
'public' => true, 'hierarchical' => true, 'has_archive' => true, 'show_ui' => true, 'show_in_menu' => 'profile.php', 'show_in_nav_menus' => true, 'rewrite' => true,but when I try to add a new post I get
“You do not have sufficient permissions to access this page
It only works if I remove ‘show_in_menu’ but it is not what I want that is ‘Profile’->’Your profile’,’CPT submenu’. How do I fix that?
-
I think you’ve found a bug! I was able to replicate your experience, so it’s not just something about your site. I think this Trac ticket is related:
https://core.trac.wordpress.org/ticket/25684The ticket was closed because the OP could not explain how to replicate the issue. I’ve dug into this a little and located the source of the issue, but a fix wasn’t immediately apparent to me. I encourage you to contribute to this ticket, explaining exactly how to reproduce the problem.
In the interim, I’m not sure about a workaround. You could try not providing the ‘show_in_menu’ argument, and use the add/remove menu/submenu page functions to get the equivalent menu layout displayed. I’ve not tried this, so I cannot know if it’ll work.
Here is what I found out regarding the bug, to help anyone else further debug this:
In wp-admin/includes/menu.phpuser_can_access_admin_page()is called and returnsfalse(line 333), causing the insufficient permissions message fromwp_die().In
user_can_access_admin_page(), it checks if the$parentvar is empty (line 1709 in wp-admin/includes/plugin.php). In our case with a ‘show_in_menu’ argument provided, there is no parent. When ‘show_in_menu’ is not used, the parent is “edit.php?post_type={$post_type_slug}”. With no parent, additional checks are made against an array of off limits menu items. One check is if the$pagenowvar (post-new.php for us) is in the off limits list withisset( $_wp_submenu_nopriv[$key][$pagenow] )(line 1719). In our case post-new.php is in the list because subscribers cannot normally edit regular posts. This causes the function to return false, resulting in thewp_die()message.A possible fix is to cause
$parentto be empty in this situation, then the extra checks are skipped. This is the case for admin users even though ‘show_in_menu’ is provided. This fix could have undesirable side effects as I’m unsure what the presence of `$parent’ really signifies.Another possible fix is to have the
$pagenowvar for CPTs to be “post-new.php?post_type={$post_type_slug}” instead of just ‘post-new.php’. The former is more accurate than the generic ‘post-new.php’ and since it does not occur in the off limits array, the additional checks with an empty$parentwill pass.I’m assuming “post-new.php?post_type={$post_type_slug}” would show up as off limits if the current user cannot edit this particular CPT. I have not verified this.
The topic ‘Add CPT submenu to profile’ is closed to new replies.