WordPress.org

Make WordPress Core

Opened 8 years ago

Last modified 2 years ago

#15550 reopened enhancement

WP_Nav_Menu_Widget needs a filter for args

Reported by: brodock Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.1
Component: Widgets Keywords: dev-feedback
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 (5)

#1 @mdawaffe
7 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.

#2 @chriscct7
4 years ago

  • Keywords dev-feedback added

#3 @DrewAPicture
4 years ago

  • Keywords widget nav_menu filter hook needs-patch removed
  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Closing as wontfix as there hasn't been any activity here in four years and I don't think I've ever seen a similar hook suggested elsewhere.

#4 @FaisalMudhir
3 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

I'd like to have this ticket reconsidered, since I also needed the same thing as the OP.

If two people mentioned wanting something chances are that there are more.

Plus having a filter around is better that not, since some people, like myself, would have create a widget that's very similar to the existing one just to be able to modify the $args parameter.

#5 @SergeyBiryukov
2 years ago

  • Milestone set to Awaiting Review
Note: See TracTickets for help on using tickets.