• Resolved Aurovrata Venet

    (@aurovrata)


    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 13 replies - 1 through 13 (of 13 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)

    Plugin Author Jeff Starr

    (@specialk)

    Okay so I’ve had some progress with this, both plugins active on default WP. I go to the Re-Order Posts settings and enable Re-Order on several post types, including custom types. Save changes and examine the sub-menus.. they all seem correct. So not sure if you found a workaround via the Re-Order plugin, or if I’m missing something..? Everything seems to be working properly, and I am unable to replicate the reported issue.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    Hello Jeff, wishing you a happy new year 2022 ??

    So not sure if you found a workaround via the Re-Order plugin, or if I’m missing something..?

    yes, that’s correct I am currently forcing the global typenow

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

    you can switch off this resest by commenting out the line 182 in the file ./reorder-posts-within-categories/includes/class-reorder-post-within-categories.php

    
    /** @since 2.9.4 reset $typenow for post admin pages. */
    $this->loader->add_action('admin_init', $plugin_admin, 'reset_typenow', PHP_INT_MAX);
    
    Plugin Author Jeff Starr

    (@specialk)

    Hi Aurovrata, likewise happy new year to you ??

    I’m glad you found a solution for this, it seems to work flawlessly for both plugins. Very interesting plugin btw, I’ve found use for it on a few sites ??

    Gonna go ahead and mark this thread as resolved. Feel free to post again with any further updates, infos, etc. Glad to help anytime.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    Very interesting plugin btw, I’ve found use for it on a few sites

    thanks, glad it is of use ??

    Gonna go ahead and mark this thread as resolved. Feel free to post again with any further updates, infos, etc. Glad to help anytime.

    ok, thanks.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘BUG: global $typenow forced to ‘post’ on edit.php admin pages’ is closed to new replies.