Question

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?

Était-ce utile?

La solution

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

Autres conseils

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top