#31675 closed enhancement (fixed)
register_sidebar() without `id` parameter should be _doing_it_wrong()
Reported by: | markjaquith | Owned by: | markjaquith |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Widgets | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
If you register a widget area with register_sidebar()
and neglect to provide an id
parameter, WordPress generates auto-increment ones for you. The id
is how WordPress stores the widget area's contents.
Consider this:
register_sidebar( array( 'One' ) ); register_sidebar( array( 'Two' ) ); register_sidebar( array( 'Three' ) );
If you comment out 'One' or 'Two', the contents of the remaining ones will get scrambled!
Best practice is to provide an 'id'. We should encourage that.
Attachments (7)
Change History (33)
#1
in reply to:
↑ description
@
10 years ago
- Keywords needs-patch good-first-bug added
#2
@
10 years ago
- Keywords has-patch added; needs-patch removed
I added a patch to trigger _doing_it_wrong() with the message the id argument is empty.
#3
@
10 years ago
- Keywords dev-feedback added
Patch 31675.2.diff changes the text a bit, and uses __FUNCTION__
not __METHOD__
as that seems to be the standard everywhere else.
#4
@
10 years ago
- Milestone changed from Awaiting Review to 4.2
- Owner set to markjaquith
- Status changed from new to accepted
Updates the text a little more. Removes the comment (the code is pretty self-explanatory).
#6
in reply to:
↑ 5
@
10 years ago
- Keywords dev-feedback removed
Replying to markjaquith:
More tweaks. Pass in 'id' as a token so it doesn't get translated.
Nice catch! 31675.3.diff looks like a good commit candidate to me.
#10
@
10 years ago
- Keywords good-first-bug removed
- Resolution fixed deleted
- Status changed from closed to reopened
#11
@
10 years ago
@miqrogroove makes the point on #31903 that if someone with an existing theme sees this notice and retroactively provides a new ID for the sidebars, then their existing content will be inaccessible.
So... this notice is useful when someone is developing a theme. We're warning them of an issue that could cause problems down the road.
If someone has already released a theme, then yes, changing the IDs to something else is going to make the contents of the old IDs inaccessible. The solution for theme authors who messed this up is to give their sidebars the auto-increment IDs that WordPress already gave them. We could, in the notice, suggest precisely this (even providing them the ID, and the "name" it corresponds to).
#12
@
10 years ago
There's a more verbose message that identifies the problematic sidebar by name, says what auto-increment id
it has defaulted to, and suggests that the dev hardcode that auto-increment id
as a manual id
, which will cement it and protect it against id
"drift".
#13
@
10 years ago
I'm wondering if this is still misleading, simply because the message doesn't say that the ID is specific to the site/network/whatever. "Manually set ... to silence this notice and keep existing sidebar content" is good advice only for a theme that wont be used on any other install, correct?
#14
@
10 years ago
Let's revert [31850] for 4.2 and start updating the args and codex to indicate that the register_sidebar args are no longer optional.
From an API point of view, you won't be able to force anyone to do this unless you add a new arg to the function. The id
would become deprecated, and its replacement such as id2
could inherit the id
value if set. If id
and id2
are both not set, then it would be possible to notify the developer to add the id2
value only. When added, the data are not lost because the id
value is unchanged.
#15
@
10 years ago
Maybe even better idea: Keep the id
arg and add a new arg called old_id_was_blank
#16
@
10 years ago
- Keywords commit added
31675.6.diff adjusts the new string just a little bit, and adds the other two placeholders to the translator comment, where %1$s is id
, %2$s is the sidebar name, %3$s is the sidebar id:
No %1$s was set in the arguments array for the "%2$s" sidebar, defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.
#17
@
10 years ago
"Manually set ... to silence this notice and keep existing sidebar content" is good advice only for a theme that wont be used on any other install, correct?
Not that I can think of. Essentially, by neglecting to choose an id
, WordPress chooses one for you. But the one WordPress chooses is based on the order you defined the sidebars.
So say a theme update adds a new sidebar, in the middle of the others. Stuff going to get jumbled. The notice tells you to set manually the id
values that WordPress is currently auto-choosing. Then, if you add a new sidebar (also with an id), the old ones don't jumble.
This ticket was mentioned in Slack in #core by drew. View the logs.
10 years ago
#19
@
10 years ago
So the sidebar id doesn't need to be globally unique? Then it makes more sense to just cement the default id.
#20
follow-up:
↓ 22
@
10 years ago
I did some testing on this issue and found several more problems:
- The new E_USER_NOTICE is essentially a nuisance error because setting the sidebar
id
to its default value does nothing. - The default value for a sidebar
id
is identical to theid
hardcoded into the bundled themes, causing the themes to share sidebars regardless whether it is specified or not. - Changing the sidebar
id
to a unique value not only causes any existing widgets to be deactivated, but may also cause any call to dynamic_sidebar() to fail because its default argument corresponds to the default sidebarid
.
I really can't find a reason for throwing a new error when the recommended action has no effect.
#21
@
10 years ago
Correction: The themes do not "share" sidebars, the widgets just get copied between them. So there really is no need for an error message in the first place.
#22
in reply to:
↑ 20
@
10 years ago
Replying to miqrogroove:
I really can't find a reason for throwing a new error when the recommended action has no effect.
There isn't a good reason for throwing a new error right now, other than that it paves the way toward actually requiring the id
parameter in the future. My +1 recommendation for the new _doing_it_wrong()
call stands.
Thank you for the feedback.
#23
follow-up:
↓ 26
@
10 years ago
- Resolution set to fixed
- Status changed from reopened to closed
In 32110:
This ticket was mentioned in Slack in #core by sergeybiryukov. View the logs.
10 years ago
#26
in reply to:
↑ 23
@
10 years ago
Replying to markjaquith:
In 32110:
Just a question about this. For an existing themes - let's say that three users have the theme in production and have many widgets in the sidebar. Their sidebars have been given some ID and in each of the three users sites that ID could be different. Now how does a theme developer set an ID that won't mess up one or more of the users?
If the answer is to query the existing value and use that, then what is the use of forcing the use of the ID, why not leave this the way it is - if a theme developer uses an ID great, if not use the auto generated value.
Just wondering.
Replying to markjaquith:
I agree. +1.