Halloween Sale 15% off Ends in See the plans
PHP

"In Menus" Column for Pages

Adds a column to the Pages list showing which navigation menus link to each page, so editors know before renaming, moving or deleting a page that it appears in the site's menus.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_menu_map = null;

add_filter( 'manage_page_posts_columns', function ( $columns ) {
    $columns['wpide_in_menus'] = 'In menus';
    return $columns;
} );

add_action( 'manage_page_posts_custom_column', function ( $column, $post_id ) use ( &$wpide_menu_map ) {
    if ( 'wpide_in_menus' !== $column ) {
        return;
    }

    // Build the page => menus map once per screen.
    if ( null === $wpide_menu_map ) {
        $wpide_menu_map = array();
        foreach ( wp_get_nav_menus() as $menu ) {
            foreach ( (array) wp_get_nav_menu_items( $menu->term_id ) as $item ) {
                if ( 'post_type' === $item->type && 'page' === $item->object ) {
                    $wpide_menu_map[ (int) $item->object_id ][ $menu->term_id ] = $menu->name;
                }
            }
        }
    }

    $menus = $wpide_menu_map[ (int) $post_id ] ?? array();
    if ( ! $menus ) {
        echo '–';
        return;
    }
    $links = array();
    foreach ( $menus as $menu_id => $name ) {
        $links[] = current_user_can( 'edit_theme_options' )
            ? '<a href="' . esc_url( admin_url( 'nav-menus.php?action=edit&menu=' . (int) $menu_id ) ) . '">' . esc_html( $name ) . '</a>'
            : esc_html( $name );
    }
    echo implode( ', ', $links ); // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above.
}, 10, 2 );

Notes

Covers classic menus (Appearance > Menus); links inside Navigation blocks of block themes are not listed.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.