WordPress.org

Make WordPress Core

Changeset 41766


Ignore:
Timestamp:
10/05/2017 01:37:39 AM (8 months ago)
Author:
SergeyBiryukov
Message:

Menus: Introduce wp_get_nav_menu_name() to retrieve the name of a navigation menu as set in the admin.

Props markoheijnen, christophherr, SpencerFinnell, wojtek.szkutnik, sagarprajapati, welcher, SergeyBiryukov.
Fixes #13910.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r41237 r41766  
    183183     */
    184184    return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
     185}
     186
     187/**
     188 * Returns the name of a navigation menu.
     189 *
     190 * @since 4.9.0
     191 *
     192 * @param string $location Menu location identifier.
     193 * @return string Menu name.
     194 */
     195function wp_get_nav_menu_name( $location ) {
     196    $menu_name = '';
     197
     198    $locations = get_nav_menu_locations();
     199
     200    if ( isset( $locations[ $location ] ) ) {
     201        $menu = wp_get_nav_menu_object( $locations[ $location ] );
     202
     203        if ( $menu && $menu->name ) {
     204            $menu_name = $menu->name;
     205        }
     206    }
     207
     208    /**
     209     * Filters the navigation menu name being returned.
     210     *
     211     * @since 4.9.0
     212     *
     213     * @param string $menu_name Menu name.
     214     * @param string $location  Menu location identifier.
     215     */
     216    return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location );
    185217}
    186218
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r41008 r41766  
    157157        $this->assertSame( $term->name, $menu_items[0]->title );
    158158        $this->assertEquals( $t, $menu_items[0]->object_id );
     159    }
     160
     161    /**
     162     * @ticket 13910
     163     */
     164    function test_wp_get_nav_menu_name() {
     165        // Register a nav menu location.
     166        register_nav_menu( 'primary', 'Primary Navigation' );
     167
     168        // Create a menu with a title.
     169        $menu = wp_create_nav_menu( 'My Menu' );
     170
     171        // Assign the menu to the `primary` location.
     172        $locations = get_nav_menu_locations();
     173        $menu_obj = wp_get_nav_menu_object( $menu );
     174        $locations['primary'] = $menu_obj->term_id;
     175        set_theme_mod( 'nav_menu_locations', $locations );
     176
     177        $this->assertEquals( 'My Menu', wp_get_nav_menu_name( 'primary' ) );
    159178    }
    160179
Note: See TracChangeset for help on using the changeset viewer.