WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 7 months ago

#19450 new defect (bug)

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

Reported by: Felipelavinz Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 3.2.1
Component: Widgets Keywords: has-patch
Focuses: Cc:

Description

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 3 years ago.
19450.2.diff (1.6 KB) - added by coffee2code 7 months ago.
Refreshed the 19450.diff patch to add unit test.

Download all attachments as: .zip

Change History (5)

@coffee2code3 years ago

comment:1 @coffee2code3 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.

comment:2 @greenshady19 months ago

  • Cc justin@… added

@coffee2code7 months ago

Refreshed the 19450.diff patch to add unit test.

comment:3 @coffee2code7 months 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.

Note: See TracTickets for help on using tickets.