WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 2 years ago

#27770 closed defect (bug) (fixed)

Can't save a widget that uses a namespace

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

Description

I am using the following plugin to provide autoload abilities: http://wordpress.org/plugins/wp-autoloader/

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 2 years ago.
Example namespaced widget class for testing
27770.diff (1.1 KB) - added by jdgrimes 2 years ago.
Unslashes widget IDs in Ajax callbacks

Download all attachments as: .zip

Change History (13)

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

Replying to Denis-de-Bernardy:

Possibly related

See #16773.

#3 @nacin
4 years ago

  • Keywords good-first-bug added

Sounds like a good thing to fix.

#4 @EFAREM
4 years ago

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

#5 @DrewAPicture
3 years ago

  • Keywords 2nd-opinion needs-testing added

#6 @allisonplus
3 years ago

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

#7 @westonruter
3 years ago

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

#8 @ZanderZ
2 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:

<?php
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 :)

@jdgrimes
2 years ago

Example namespaced widget class for testing

@jdgrimes
2 years ago

Unslashes widget IDs in Ajax callbacks

#9 @jdgrimes
2 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
2 years ago

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

Slasheeeeeeesssssss

#11 @johnbillion
2 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.