Prevent error in Menu location checkbox settings#10859
Prevent error in Menu location checkbox settings#10859sabernhardt wants to merge 2 commits intoWordPress:trunkfrom
Conversation
…bedby` for checkboxes
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| /* translators: %s: Menu name. */ | ||
| _x( '(Currently set to: %s)', 'menu location' ), | ||
| wp_get_nav_menu_object( $menu_locations[ $location ] )->name | ||
| 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.
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.
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.
Make sense. Thanks for details.
|
@sabernhardt Request some changes on PR. |
| foreach ( $locations as $location => $description ) : | ||
| $checked = false; | ||
| $checked = false; | ||
| $aria_describedby = ''; |
There was a problem hiding this comment.
The $aria_describedby variable is defined at the top of the foreach loop. When I tried setting the variable conditionally, checking isset() pulled values from other display locations (the variable was set earlier in the loop).
| <?php if ( '' !== $aria_describedby ) : ?> | ||
| aria-describedby="<?php echo esc_attr( $aria_describedby ); ?>" | ||
| <?php endif; ?> | ||
| /> |
There was a problem hiding this comment.
Yes, this input is still self-closing. That helps a little to identify where the multi-line tag ends. Until WordPress replaces all of the self-closing tags, I want to keep this here.
There was a problem hiding this comment.
Pull request overview
Updates the nav menus admin screen to avoid errors when a theme location is assigned to an invalid menu ID, and improves accessibility by associating the “currently set to” text with the location checkbox via aria-describedby.
Changes:
- Add fallback display text when a theme location points to an invalid menu ID (avoids fatal when accessing
->name). - Add
idto the “currently set to”<span>and conditionally addaria-describedbyon the corresponding checkbox.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| _x( '(Currently set to: %s)', 'menu location' ), | ||
| wp_get_nav_menu_object( $menu_locations[ $location ] )->name | ||
| 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.
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.
| <span class="theme-location-set" id="theme-location-set-<?php echo esc_attr( $location ); ?>"> | ||
| <?php | ||
| printf( | ||
| /* translators: %s: Menu name. */ |
There was a problem hiding this comment.
The translator comment says %s is a menu name, but in the invalid-ID case %s becomes a fallback message instead. Please update the translator comment (or refactor) so it accurately describes all possible substitution values.
| /* translators: %s: Menu name. */ | |
| /* translators: %s: Menu name or a message indicating an invalid menu ID. */ |
There was a problem hiding this comment.
The text would depend on whether the message changes, but possibly:
Menu name or a message indicating that the menu was not found.
| /* translators: %s: Menu name. */ | ||
| _x( '(Currently set to: %s)', 'menu location' ), | ||
| wp_get_nav_menu_object( $menu_locations[ $location ] )->name | ||
| 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.
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.
This is not necessary. Core translations are considered trusted.
There was a problem hiding this comment.
Oh wait, nevermind, the name is not a translation string. So yes, this is good feedback.
| 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.
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.
another possibility: 'a nonexistent menu'
| if ( isset( $menu_locations[ $location ] ) | ||
| && 0 !== $nav_menu_selected_id | ||
| && $menu_locations[ $location ] === $nav_menu_selected_id | ||
| ) { | ||
| $checked = true; | ||
| } |
There was a problem hiding this comment.
The assignment inside this if block is over-indented compared to surrounding control structures in this file, which breaks WP coding style and makes diffs noisier. Please fix indentation so the block contents are indented exactly one level inside the if.
| if ( ! empty( $menu_locations[ $location ] ) | ||
| && $menu_locations[ $location ] !== $nav_menu_selected_id | ||
| ) { | ||
| $aria_describedby = "theme-location-set-$location"; | ||
| } |
There was a problem hiding this comment.
Same indentation issue here: $aria_describedby = "theme-location-set-$location"; is indented deeper than expected for the body of the if block. Adjust indentation to match the rest of the file's style.


idandaria-describedbyto the connect thespanwith the checkboxes.Trac 37026
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.