Skip to content

Commit fbd8c72

Browse files
Merge pull request #3852 from diggy/improve-wp-menu-location-assign
Improve `wp menu location assign` command
2 parents a885a80 + 6b94db1 commit fbd8c72

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

features/menu-location.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,28 @@ Feature: Manage WordPress menu locations
2929
Then STDOUT should be a table containing rows:
3030
| slug | locations |
3131
| primary-menu | |
32+
33+
When I try `wp menu location assign secondary-menu secondary`
34+
Then STDERR should be:
35+
"""
36+
Error: Invalid menu secondary-menu.
37+
"""
38+
39+
When I run `wp menu create "Secondary Menu"`
40+
And I try `wp menu location assign secondary-menu secondary`
41+
Then STDERR should be:
42+
"""
43+
Error: Invalid location secondary.
44+
"""
45+
46+
When I run `wp menu location assign secondary-menu primary`
47+
Then STDOUT should be:
48+
"""
49+
Success: Assigned location primary to menu secondary-menu.
50+
"""
51+
52+
When I run `wp menu list --fields=slug,locations`
53+
Then STDOUT should be a table containing rows:
54+
| slug | locations |
55+
| primary-menu | |
56+
| secondary-menu | primary |

php/commands/menu-location.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,30 @@ function($o) {
100100
* ## EXAMPLES
101101
*
102102
* $ wp menu location assign primary-menu primary
103-
* Success: Assigned location to menu.
103+
* Success: Assigned location primary to menu primary-menu.
104104
*
105105
* @subcommand assign
106106
*/
107107
public function assign( $args, $_ ) {
108108

109109
list( $menu, $location ) = $args;
110110

111-
$menu = wp_get_nav_menu_object( $menu );
112-
if ( ! $menu || is_wp_error( $menu ) ) {
113-
WP_CLI::error( "Invalid menu." );
111+
$menu_obj = wp_get_nav_menu_object( $menu );
112+
if ( ! $menu_obj ) {
113+
WP_CLI::error( "Invalid menu $menu." );
114114
}
115115

116116
$locations = get_registered_nav_menus();
117117
if ( ! array_key_exists( $location, $locations ) ) {
118-
WP_CLI::error( "Invalid location." );
118+
WP_CLI::error( "Invalid location $location." );
119119
}
120120

121121
$locations = get_nav_menu_locations();
122-
$locations[ $location ] = $menu->term_id;
122+
$locations[ $location ] = $menu_obj->term_id;
123123

124124
set_theme_mod( 'nav_menu_locations', $locations );
125125

126-
WP_CLI::success( "Assigned location to menu." );
126+
WP_CLI::success( "Assigned location $location to menu $menu." );
127127
}
128128

129129
/**

0 commit comments

Comments
 (0)