Skip to content

Commit 9248e4c

Browse files
author
Peter J. Herrel
committed
Improve wp menu location assign command
1 parent 264ac0d commit 9248e4c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

features/menu-location.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ 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+
And I run `wp menu list --fields=slug,locations`
48+
Then STDOUT should be a table containing rows:
49+
| slug | locations |
50+
| primary-menu | |
51+
| secondary-menu | primary |

php/commands/menu-location.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,22 @@ 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)