[wp-trac] [WordPress Trac] #29714: user_can_access_admin_page() returning false for edit.php?post_type=CPT

WordPress Trac noreply at wordpress.org
Wed Nov 5 22:37:07 UTC 2014


#29714: user_can_access_admin_page() returning false for edit.php?post_type=CPT
----------------------------------------+------------------------------
 Reporter:  bobbingwide                 |       Owner:
     Type:  defect (bug)                |      Status:  new
 Priority:  normal                      |   Milestone:  Awaiting Review
Component:  Role/Capability             |     Version:  4.0
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:
----------------------------------------+------------------------------

Comment (by johnrom):

 In the example workaround above, $pagenow will be changed again the next
 time user_has_cap is called. I would switch to the "custom_menu_order"
 hook as it is only called once.

 Alternately, I came up with a similar workaround before I found this
 ticket (I found this when writing out a TRAC ticket, "similar threads" is
 very useful). Instead of manipulating something so mission critical as
 $pagenow, I added a menu item to the submenu exactly where the check would
 expect it to be, and then removed it right after the checkpoint.


 {{{
 /**
  * Workaround for TRAC #29714
  * @hooked custom_menu_order
  */
 function hack_menu_to_allow_add_child_cpt( $custom_menu_order ) {
         global $submenu;

         $parent_post_type = 'cpt';
         $my_post_type = 'child_cpt';

         $my_post_type_object = get_post_type_object( $my_post_type );
         $create_cpt_capability = $my_post_type_object->cap->create_posts;

         $parent_post_type_edit = "edit.php?post_type={$parent_post_type}";
         $child_post_type_new = "post-new.php?post_type={$my_post_type}";

         if ( ! empty( $submenu[ $parent_post_type_edit ] ) ) {
                 $submenu[ $parent_post_type_edit ][] = array(
                         'Add Child CPT',
                         $create_cpt_capability,
                         "post-new.php?post_type={$my_post_type}",
                         'New Child CPT',
                 );
         }

         // we didn't do anything with this
         return $custom_menu_order;
 }
 add_filter('custom_menu_order', 'hack_menu_to_allow_add_child_cpt');


 /**
  * Part 2: Workaround for TRAC #29714
  * @hooked add_menu_classes
  */
 function hack_unset_menu_item_after_child_cpt_perms( $menu ) {
         global $submenu;

         $parent_post_type = 'cpt';
         $my_post_type = 'child_cpt';

         $parent_post_type_edit = "edit.php?post_type={$parent_post_type}";
         $child_post_type_new = "post-new.php?post_type={$my_post_type}";

         if ( ! empty( $submenu[ $parent_post_type_edit ] ) ) {
                 foreach( $submenu[ $parent_post_type_edit ] as
 $submenu_item_key => $submenu_item ) {

                         if ( $child_post_type_new == $submenu_item[2] )
                                 unset( $submenu[ $parent_post_type_edit ][
 $submenu_item_key ] );
                 }
         }

         // we didn't do anything with this either
         return $menu;
 }
 add_filter('add_menu_classes',
 'hack_unset_menu_item_after_child_cpt_perms');

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29714#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list