Support » Plugin: Disable Gutenberg » BUG: global $typenow forced to ‘post’ on edit.php admin pages

  • Greetings,

    I am an author of a plugin that adds a sub-menu to the Posts dashboard menu.

    
    add_submenu_page('edit.php', 'Re-order', 'Reorder', $capability, 're-order', 'print_order_page');
    

    this sub-menu page is capability dependent and users can configure the plugin so as to give access to this page to roles other than an administrator.

    The WP function add_submenu_page the proceeds hook the sub-menu page to the callback function. this hook is created by WP core and is associated with the current post type stored in the global $typenow variable.

    For post, the hook is associated with the parent slug edit.php and the $typenow=''.

    For custom post types, the hook is associated with the parent slug edit.php?post_type={$typenow}.

    The Disable gutenberg inc/plugin-core.php file on line 177 forces the $typenow = ‘post’ on the edit.php page,

    if ($pagenow === 'edit.php') {
      if (empty($typenow)) $typenow = 'post';
      $post_type = $typenow;
    }

    this causes WP to fail to detect that the current type is post, and therefore associates the current sub-menu hook with a custom post type, breaking the core functionality.

    the above code is only called for amdin page requests from users have non-administrator roles.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jeff Starr

    (@specialk)

    Thanks for the detailed report, I will investigate and try to resolve any issue. Also just to be clear, the reported bug is triggered when a non-admin user takes which steps exactly? I need to be able to replicate the issue on default WordPress. Thanks again.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    hey @specialk thanks for the quick reply,

    the reported bug is triggered when a non-admin user takes which steps exactly?

    so, if a sub-menu page is added with a capability lower than an admin role (editor or author), then the sub-menu appears on the admin menu.

    However, when a non-admin user requests the sub-menu page it is inaccessible.

    The hook registered for the callback function passed in the add_submenu_page is no longer found as the $typenow variable impacts the way the hook id is generated by WP core.

    When the sub-menu page is registered the $typenow is blank (for post type) and the hook id generated by WP core is as expected. However, when the page is requested (admin_init action if fired and this time $typenow is set to ‘post’) WP core generates a hook id which is different, and therefore the callback function is not found, resulting in wp_die blank page.

    The only way round to this problem for now is to hook the admin_init last and reset the typenow,

    add_action('admin_init', 'reset_typenow', PHP_INT_MAX);
    function reset_typenow(){
      global $typenow, $pagenow;
      if('edit.php' == $pagenow && 'post' == $typenow) $typenow='';
    }

    however, I am not sure how this may affect your plugin.

    Plugin Author Jeff Starr

    (@specialk)

    Thanks that is very helpful. I will use this infos and try to investigate and resolve the issue if possible for the next plugin update.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    however, I am not sure how this may affect your plugin.

    fyi, I tested the plugin and it still disables gutenberg on post edit page page, showing the classic editor, despite resetting the $typenow as per my work around.

    Plugin Author Jeff Starr

    (@specialk)

    Just following up with this, I’ve not been able to find a solution. If anyone knows of a way to handle the issue described above, please let us know. Thank you.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    The Disable gutenberg inc/plugin-core.php file on line 177 forces the $typenow = ‘post’ on the edit.php page,

    could you not simply remove that line altogether? It does not seem to impact the plugin’s functionality in any way as far as I can tell.

    Plugin Author Jeff Starr

    (@specialk)

    It may be possible, will give it a try next time around. Thanks @aurovrata.

    Plugin Author Jeff Starr

    (@specialk)

    Hey I am looking into this, can you let me know which other plugin you are using, so I can test, etc. Thank you.

    Thread Starter Aurovrata Venet

    (@aurovrata)

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.