Make WordPress Core

Opened 12 years ago

Closed 9 years ago

Last modified 9 years ago

#19450 closed defect (bug) (fixed)

the_widget won't replace the widget class if not using the default sidebar arguments

Reported by: felipelavinz's profile Felipelavinz Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: minor Version: 3.2.1
Component: Widgets Keywords: has-patch
Focuses: Cc:


When using the_widget(), the $before_widget argument only receives the widget class if using the default sidebar arguments...

on lines 1129 - 1130 of wp-includes/widgets.php (wp 3.3 rc 1)

$before_widget = sprintf('<div class="widget %s">', $widget_obj->widget_options['classname'] );
$default_args = array( 'before_widget' => $before_widget, 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' );

$args = wp_parse_args($args, $default_args);

could be replaced by:

$default_args = array( 'before_widget' => '<div class="widget %s">', 'after_widget' => "</div>", 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' );

$args = wp_parse_pargs($args, $default_args);
$args['before_widget'] = sprintf($args['before_widget'], $widget_obj->widget_options['classname']);

Attachments (2)

19450.diff (1013 bytes) - added by coffee2code 12 years ago.
19450.2.diff (1.6 KB) - added by coffee2code 10 years ago.
Refreshed the 19450.diff patch to add unit test.

Download all attachments as: .zip

Change History (8)

12 years ago

#1 @coffee2code
12 years ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Awaiting Review to Future Release
  • Version set to 3.2.1

Added 19450.diff.

As originally reported, it is true that when using the_widget(), if you override the before_widget arg for the widget, there is no way to gain the benefit of auto-assignment of the default classname. The default before_widget value is defined as <div class="widget %s">. However, the %s gets replaced prior to use in the defaults arg array (and thus prior to being passed through wp_parse_args() to determine the final arg array).

The patch, as originally proposed by @Felipelavinz, simply delays the sprintf() substitution until after the wp_parse_args() completes, allowing the following example to work properly:

the_widget('WP_Widget_Pages', 'title=Contents&sortby=post_modified', 'before_widget=<div class="special %s">&before_title=<span>&after_title=</span>');

There should be no compatibility issues or any other drawbacks to its implementation.

#2 @greenshady
11 years ago

  • Cc justin@… added

10 years ago

Refreshed the 19450.diff patch to add unit test.

#3 @coffee2code
10 years ago

Added 19450.2.diff as a refresh of 19450.diff, namely to add a unit test.

As stated before, most of the change is whitespace. The patch essentially just moves the execution of sprintf() for the 'before_widget' value until after the call to wp_parse_args() to allow any customized 'before_widget' string to still get the benefit of the auto-assignment of the default classname by including '%s' in the string.

#4 @wonderboymusic
9 years ago

  • Milestone changed from Future Release to 4.4

#5 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 35106:

Widgets: When using the_widget(), the $before_widget argument only receives the widget class if using the default sidebar arguments. Run sprintf after parsing the args to fix this.

Adds unit test.

Props coffee2code.
Fixes #19450.

#6 @wonderboymusic
9 years ago

In 35113:

Widgets: after [35106], ensure that the widget required by the unit test is registered.

See #19450.

Note: See TracTickets for help on using tickets.