-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Prevent error in Menu location checkbox settings #10859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1200,25 +1200,39 @@ function wp_nav_menu_max_depth( $classes ) { | |||||||||||||
| <legend class="menu-settings-group-name howto"><?php _e( 'Display location' ); ?></legend> | ||||||||||||||
| <?php | ||||||||||||||
| foreach ( $locations as $location => $description ) : | ||||||||||||||
| $checked = false; | ||||||||||||||
| $checked = false; | ||||||||||||||
| $aria_describedby = ''; | ||||||||||||||
|
|
||||||||||||||
| if ( isset( $menu_locations[ $location ] ) | ||||||||||||||
| && 0 !== $nav_menu_selected_id | ||||||||||||||
| && $menu_locations[ $location ] === $nav_menu_selected_id | ||||||||||||||
| ) { | ||||||||||||||
| $checked = true; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
1206
to
1211
|
||||||||||||||
|
|
||||||||||||||
| if ( ! empty( $menu_locations[ $location ] ) | ||||||||||||||
| && $menu_locations[ $location ] !== $nav_menu_selected_id | ||||||||||||||
| ) { | ||||||||||||||
| $aria_describedby = "theme-location-set-$location"; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+1213
to
+1217
|
||||||||||||||
| ?> | ||||||||||||||
| <div class="menu-settings-input checkbox-input"> | ||||||||||||||
| <input type="checkbox"<?php checked( $checked ); ?> name="menu-locations[<?php echo esc_attr( $location ); ?>]" id="locations-<?php echo esc_attr( $location ); ?>" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" /> | ||||||||||||||
| <input type="checkbox"<?php checked( $checked ); ?> | ||||||||||||||
| name="menu-locations[<?php echo esc_attr( $location ); ?>]" | ||||||||||||||
| id="locations-<?php echo esc_attr( $location ); ?>" | ||||||||||||||
| value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" | ||||||||||||||
| <?php if ( '' !== $aria_describedby ) : ?> | ||||||||||||||
| aria-describedby="<?php echo esc_attr( $aria_describedby ); ?>" | ||||||||||||||
| <?php endif; ?> | ||||||||||||||
| /> | ||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this |
||||||||||||||
| <label for="locations-<?php echo esc_attr( $location ); ?>"><?php echo $description; ?></label> | ||||||||||||||
| <?php if ( ! empty( $menu_locations[ $location ] ) && $menu_locations[ $location ] !== $nav_menu_selected_id ) : ?> | ||||||||||||||
| <span class="theme-location-set"> | ||||||||||||||
| <?php if ( '' !== $aria_describedby ) : ?> | ||||||||||||||
| <span class="theme-location-set" id="theme-location-set-<?php echo esc_attr( $location ); ?>"> | ||||||||||||||
| <?php | ||||||||||||||
| printf( | ||||||||||||||
| /* translators: %s: Menu name. */ | ||||||||||||||
|
||||||||||||||
| /* translators: %s: Menu name. */ | |
| /* translators: %s: Menu name or a message indicating an invalid menu ID. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text would depend on whether the message changes, but possibly:
Menu name or a message indicating that the menu was not found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using a similar defensive approach as used here: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/nav-menus.php#L643-L646
$menu_name = __( 'an invalid menu ID' );
if ( is_nav_menu( $menu_locations[ $location ] ) ) {
$menu_object = wp_get_nav_menu_object( $menu_locations[ $location ] );
if ( ! is_wp_error( $menu_object ) ) {
$menu_name = $menu_object->name;
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_nav_menu() function already checks for an error from wp_get_nav_menu_object(), and it also makes sure it is not false or an incorrect taxonomy (as noted on the ticket).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. Thanks for details.
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses is_nav_menu() and then calls wp_get_nav_menu_object() again, which results in duplicate lookups (since is_nav_menu() already calls wp_get_nav_menu_object()). Consider fetching the menu object once, validating it, and then using its name to avoid redundant work and simplify the expression.
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The menu name is output without escaping (->name is printed directly into HTML). Please escape the computed display string for the %s placeholder (e.g., esc_html(...)) so the output is safe and consistent with WP output-escaping practices.
| is_nav_menu( $menu_locations[ $location ] ) ? wp_get_nav_menu_object( $menu_locations[ $location ] )->name : __( 'an invalid menu ID' ) | |
| esc_html( | |
| is_nav_menu( $menu_locations[ $location ] ) | |
| ? wp_get_nav_menu_object( $menu_locations[ $location ] )->name | |
| : __( 'an invalid menu ID' ) | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary. Core translations are considered trusted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, nevermind, the name is not a translation string. So yes, this is good feedback.
Copilot
AI
Feb 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback text an invalid menu ID is user-facing and uses internal jargon (“ID”). Consider a more user-friendly string (and possibly including the numeric ID for debugging) so admins understand what’s wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another possibility: 'a nonexistent menu'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or just "unknown menu"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
$aria_describedbyvariable is defined at the top of theforeachloop. When I tried setting the variable conditionally, checkingisset()pulled values from other display locations (the variable was set earlier in the loop).