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

Desired output with Main menu active

Current output

Current output with no trace of current page in admin menu

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

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?

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' ); }); Commented Dec 18, 2019 at 10:00

2 Answers 2

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;
}
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.