最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What's the correct way of moving a menu page to a submenu

programmeradmin5浏览0评论

In one of my client sites, the default comment feature is not needed globally. Instead I'm using it only under a certain CPT (eg. Complaints). With the following code, I moved the parent menu item: "Comments" under the "Complaints" CPT:

<?php
/**
 * Relocate Comments in Admin Menu.
 *
 * Relocate Comments parent menu under a CPT.
 */
function wpse354847_relocate_comments_in_admin_menu()
{
    // Remove existing parent menu.
    remove_menu_page( 'edit-comments.php' );

    // Move Comments under Complaints ('complaint').
    add_submenu_page(
        'edit.php?post_type=complaint', //$parent_slug
        __( 'Replies', 'wpse354847' ), //$page_title
        __( 'Replies', 'wpse354847' ), //$menu_title
        'edit_posts', //$capability
        'edit-comments.php' //$menu_slug
    );
}

add_action( 'admin_menu', 'wpse354847_relocate_comments_in_admin_menu' );

But the issue is: when I'm on the "Comments" page the parent menu is not get selected. I found that, two HTML classes are responsible for the CSS to be triggered: .wp-has-current-submenu and .wp-menu-open.

Desired output

Current output

After some searching I found some Javascript approaches to resolve the issue - like:

  • Moving Categories submenu to Media, but still opens Posts menu - WPSE
  • How to manually set a custom admin submenu selected? - WPSE

But I'm not convinced with them, as I might be wrong when I'm repositioning the Comments menu page as a submenu where the native active menu classes are not loading.

Hence I'm asking here: am I on the right track? Is Javascript the only last resort to solve the issue?

In one of my client sites, the default comment feature is not needed globally. Instead I'm using it only under a certain CPT (eg. Complaints). With the following code, I moved the parent menu item: "Comments" under the "Complaints" CPT:

<?php
/**
 * Relocate Comments in Admin Menu.
 *
 * Relocate Comments parent menu under a CPT.
 */
function wpse354847_relocate_comments_in_admin_menu()
{
    // Remove existing parent menu.
    remove_menu_page( 'edit-comments.php' );

    // Move Comments under Complaints ('complaint').
    add_submenu_page(
        'edit.php?post_type=complaint', //$parent_slug
        __( 'Replies', 'wpse354847' ), //$page_title
        __( 'Replies', 'wpse354847' ), //$menu_title
        'edit_posts', //$capability
        'edit-comments.php' //$menu_slug
    );
}

add_action( 'admin_menu', 'wpse354847_relocate_comments_in_admin_menu' );

But the issue is: when I'm on the "Comments" page the parent menu is not get selected. I found that, two HTML classes are responsible for the CSS to be triggered: .wp-has-current-submenu and .wp-menu-open.

Desired output

Current output

After some searching I found some Javascript approaches to resolve the issue - like:

  • Moving Categories submenu to Media, but still opens Posts menu - WPSE
  • How to manually set a custom admin submenu selected? - WPSE

But I'm not convinced with them, as I might be wrong when I'm repositioning the Comments menu page as a submenu where the native active menu classes are not loading.

Hence I'm asking here: am I on the right track? Is Javascript the only last resort to solve the issue?

Share Improve this question asked Dec 18, 2019 at 9:36 Mayeenul IslamMayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges 2
  • Have you tried removing comment support for the default post type and adding it for the custom post type? That should remove the comment submenu from the default post type, but leave it under the custom post type. – Johansson Commented Dec 18, 2019 at 9:55
  • Added the 'comments' support to the CPT when registered. But now, tried removing the support from the default post type post. But no luck. add_action('init', function() { remove_post_type_support( 'post', 'comments' ); }); – Mayeenul Islam Commented Dec 18, 2019 at 10:00
Add a comment  | 

2 Answers 2

Reset to default 2

By adding post_type to add_submenu_page menu slug it will active CPT page menu. then you have to add parent page as that CPT to that commnet page by using submenu_file filter.

# Move comment to CPT

function wpse354847_relocate_comments_in_admin_menu()
{
    // Remove existing parent menu.
    remove_menu_page( 'edit-comments.php' );

    // Move Comments under Complaints ('complaint').
    add_submenu_page(
        'edit.php?post_type=complaint', //$parent_slug
        __( 'Replies', 'wpse354847' ), //$page_title
        __( 'Replies', 'wpse354847' ), //$menu_title
        'edit_posts', //$capability
        'edit-comments.php?post_type=complaint' //$menu_slug
    );
}
add_action( 'admin_menu', 'wpse354847_relocate_comments_in_admin_menu' );

# add active page for parent page

add_filter('submenu_file', 'menuBold');
function menuBold($submenu_file)
{
    global $pagenow;
    if (( $pagenow == 'edit-comments.php' ) && ($_GET['post_type'] == 'complaint')) {   // The address of the link to be highlighted
        return 'edit-comments.php?post_type=complaint';
    }
    // Don't change anything
    return $submenu_file;
}

the first step is to set edit-comments.php?post_type=complaint for the menu slug.

and then you add this hook

add_filter("submenu_file", function ($submenu_file, $parent_file) {

    $screen = get_current_screen();

    if ("edit-comments" === $screen->id) {
        $submenu_file = "edit-comments.php?post_type=$screen->post_type";
    }

    return $submenu_file;

}, 10, 2);
发布评论

评论列表(0)

  1. 暂无评论