Make WordPress Core

Opened 10 years ago

Closed 7 years ago

#27770 closed defect (bug) (fixed)

Can't save a widget that uses a namespace

Reported by: pcfreak30's profile pcfreak30 Owned by: johnbillion's profile johnbillion
Milestone: 4.9 Priority: normal
Severity: normal Version: 3.8.2
Component: Widgets Keywords: good-first-bug needs-testing has-patch
Focuses: Cc:


I am using the following plugin to provide autoload abilities:

I have created a widget that uses the namespace '\WordPress\Widgets'. I was able to use register_widget with it.

However upon adding and saving the widget, I refresh and it is not stored. Removing the namespacing and doing a manual require allows it to function as intended.

Attachments (2)

example-widget.php (2.1 KB) - added by jdgrimes 7 years ago.
Example namespaced widget class for testing
27770.diff (1.1 KB) - added by jdgrimes 7 years ago.
Unslashes widget IDs in Ajax callbacks

Download all attachments as: .zip

Change History (13)

#2 in reply to: ↑ 1 @jdgrimes
9 years ago

Replying to Denis-de-Bernardy:

Possibly related

See #16773.

#3 @nacin
9 years ago

  • Keywords good-first-bug added

Sounds like a good thing to fix.

8 years ago

Just tested in nightly build and it seems to work fine with and without the leading slash.

#5 @DrewAPicture
8 years ago

  • Keywords 2nd-opinion needs-testing added

#6 @allisonplus
7 years ago

Tested in trunk and cannot replicate. Widget saved fine, regardless of NameSpacing.

#7 @westonruter
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

#8 @ZanderZ
7 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I also ran into this problem today and can give more info on what's causing it.

This is my code:

register_widget(new OurNamespace\SocialWidget);

When adding this widget to a sidebar, a POST call is made to admin_ajax.php, with payload (Example)

"widget_id": "widget-ournamespace\socialwidget-2",
"id_base": "ournamespace\socialwidget"

The slash is causing the problem. When you call parent::__construct with an id_base without a slash (I used ournamespace_socialwidget), the problem is gone.

Hope that helps you! If more info is useful, feel free to ask :)

7 years ago

Example namespaced widget class for testing

7 years ago

Unslashes widget IDs in Ajax callbacks

#9 @jdgrimes
7 years ago

  • Keywords has-patch added; 2nd-opinion removed

I've attached a basic namespaced widget in example-widget.php, for testing purposes, and come up with 27770.diff, which fixes the issue for me.

#10 @johnbillion
7 years ago

  • Milestone set to 4.9
  • Owner set to johnbillion
  • Status changed from reopened to reviewing


#11 @johnbillion
7 years ago

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

In 41292:

Widgets: Correctly strip slashes from widget and sidebar names to allow namespaced widgets to function correctly.

Fixes #27770

Props jdgrimes

Note: See TracTickets for help on using tickets.