"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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.