WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#4910 closed defect (bug) (fixed)

Widgets might get "Array" as CSS class instead of classname

Reported by: f00f Owned by:
Milestone: 2.3 Priority: high
Severity: minor Version: 2.3
Component: General Keywords: widgets css class has-patch dev-reviewed
Focuses: Cc:

Description

When register_sidebar_widget is called with two parameters only and the second one is an array, like

register_sidebar_widget(__('WdgName'), array('WdgClass', 'Method'));

then the CSS class attribute of the widget will contain 'Array' where it should contain the class_name.

Fix: implode classname if it's an array.

File: widgets.php
Line: Replace line 213 with those two lines:

$classname_ = is_array($wp_registered_widgets[$id]['classname']) ? implode('_', $wp_registered_widgets[$id]['classname']) : $wp_registered_widgets[$id]['classname'];
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

Btw: I do know (now) that there is a third parameter to register_sidebar_widget(), but we'd better be save than sorry.

The fix should be absolutely save because only local variable is affected.
Sorry for not providing a diff, how do I create one? (Win32)
Easy fix, please include in 2.3 kthxbye ;)

Attachments (1)

4910.003.diff (933 bytes) - added by markjaquith 7 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 markjaquith7 years ago

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

(In [6054]) Properly construct widget classname if second param is an array. Props f00f. fixes #4910

comment:2 mattyrob7 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

This changeset has broken my widget called from within a class as follows:

register_sidebar_widget('Subscribe2Widget', array(&$this, 'widget_subscribe2widget'));

With the following error:

Catchable fatal error: Object of class s2class could not be converted to string in /Users/Matthew/Sites/trunk/wp-includes/widgets.php on line 213

A fix appears to be as follows:

$classname_ = ( is_array($wp_registered_widgets[$id]['classname']) ) ? $wp_registered_widgets[$id]['classname'][1] : $wp_registered_widgets[$id]['classname'];

comment:3 markjaquith7 years ago

I don't like that, because it only uses the method name, which could be quite generic.

How about this version? If passed a string, it uses it. If passed an array with a class name and method, it uses them (with _ in the middle). If passed an array with an object reference and method, it extracts the object's class name and uses that plus the method (with _ in the middle).

markjaquith7 years ago

comment:4 andy7 years ago

Sweet.

comment:5 westi7 years ago

  • Keywords dev-reviewed added

that new patch looks good and should solve this issue.

comment:6 in reply to: ↑ description mattyrob7 years ago

New patch works for me.

comment:7 markjaquith7 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [6098]) Set widget classname when passed string, object/method, object-reference/method. fixes #4910

Note: See TracTickets for help on using tickets.