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?

  • 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. – Jack Johansson Dec 18 '19 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 Dec 18 '19 at 10:00
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;
}
| improve this answer | |
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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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