WP_Customize_Nav_Menus::filter_wp_nav_menu() WordPress Method

The WP_Customize_Nav_Menus::filter_wp_nav_menu() method is used to filter the nav menu used in the Customizer. This is useful for creating custom nav menus for the Customizer.

WP_Customize_Nav_Menus::filter_wp_nav_menu( string $nav_menu_content, object $args ) #

Prepares wp_nav_menu() calls for partial refresh.


Description

Injects attributes into container element.

Top ↑

See also


Top ↑

Parameters

$nav_menu_content

(string)(Required)The HTML content for the navigation menu.

$args

(object)(Required)An object containing wp_nav_menu() arguments.


Top ↑

Return

(string) Nav menu HTML with selective refresh attributes added if partial can be refreshed.


Top ↑

Source

File: wp-includes/class-wp-customize-nav-menus.php

	public function filter_wp_nav_menu( $nav_menu_content, $args ) {
		if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) {
			$attributes       = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
			$attributes      .= ' data-customize-partial-type="nav_menu_instance"';
			$attributes      .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
			$nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . str_replace( '\\', '\\\\', $attributes ), $nav_menu_content, 1 );
		}
		return $nav_menu_content;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.