WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#15550 new enhancement

WP_Nav_Menu_Widget needs a filter for args

Reported by: brodock Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Widgets Keywords: widget nav_menu filter hook needs-patch
Focuses: Cc:

Description

I have a very common need to change the walker of a menu printed by WP_Nav_Menu_Widget. The only way to do this is by injecting a new walker on the args array.

By so, I propose changing default-widgets.php:

function widget($args, $instance) {

	// Get menu
	$nav_menu = wp_get_nav_menu_object( $instance['nav_menu'] );

	if ( !$nav_menu )
		return;

	$instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);

	echo $args['before_widget'];

	if ( !empty($instance['title']) )
		echo $args['before_title'] . $instance['title'] . $args['after_title'];

	wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );

	echo $args['after_widget'];
}

To:

function widget($args, $instance) {

	// Filter for args
	$args = apply_filters('nav_manu_widget_args', $args);

	// Get menu
	$nav_menu = wp_get_nav_menu_object( $instance['nav_menu'] );

	if ( !$nav_menu )
		return;

	$instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);

	echo $args['before_widget'];

	if ( !empty($instance['title']) )
		echo $args['before_title'] . $instance['title'] . $args['after_title'];

	wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );

	echo $args['after_widget'];
}

Change History (1)

comment:1 mdawaffe3 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

How about we change

$instance = apply_filters('widget_display_callback', $instance, $this, $args);
...
do_action( 'the_widget', $widget, $instance, $args );

to use apply_filters_ref_array() and do_action_ref_array(). Or maybe better: add a new ref_array action near those points.

Note: See TracTickets for help on using tickets.