Make WordPress Core

Opened 4 years ago

Last modified 15 months ago

#12133 new enhancement

get_field_id() and get_field_name() break when passed a name in array format

Reported by: johnbillion Owned by: azaozz
Milestone: Future Release Priority: normal
Severity: minor Version: 2.9.1
Component: Widgets Keywords: needs-patch
Focuses: Cc:


get_field_name() and get_field_id() are member functions of the widget API which generate the names and ids for fields on the widget admin screen.

A custom widget cannot use an array format name with this function.

For example, 'myfield[]' or 'myfield[3]' is a valid name for an input field, but this name cannot be passed to get_field_name() as the function will strip right square brackets from the name.

Patch upcoming when I find the time.

Attachments (1)

patch.diff (1.1 KB) - added by ch1902 15 months ago.

Download all attachments as: .zip

Change History (7)

comment:1 scribu4 years ago

  • Cc scribu@… added

comment:2 scribu4 years ago

  • Milestone changed from Unassigned to 3.0

comment:3 nacin4 years ago

  • Keywords needs-patch added; get_field_name get_field_id widgets removed
  • Milestone changed from 3.0 to Future Release

Needs patch, sending to future release for now.

ch190215 months ago

comment:4 ch190215 months ago

Not sure if I've done this right, it's my first patch.

This fixes the array style input names and makes get_field_id() produce tidier HTML id attributes. I realise HTML5 id attributes are much more flexible in what they accept than HTML4, but I think simpler IDs are better for jQuery/CSS selectors.

comment:5 scribu15 months ago

1) In get_field_id():

str_replace(array('][', '[', ']', ' '), '-', trim($field_name, ' []')

can be simplified to this:

strtr('[] ', '-', $field_name)

or maybe even


2) I don't think the logic in get_field_name() is correct. If $field_name is 'foo[bar]', you'll end up with nested brackets: [$this->id][foo[bar]] which will not produce correct values in $_POST.

3) Tabs, not spaces http://make.wordpress.org/core/handbook/coding-standards/#indentation

comment:6 ch190215 months ago

1) No, but it could be simplified to

strtr($field_name, '[] ', '---')

However that produces less than attractive IDs such as "a-b--c-" from "a[b][c]" instead of my version which produces "a-b-c". I have no opinion on sanitize_title_with_dashes because I don't know much of the internals of Wordpress and was sticking to pure PHP.

2) The logic is correct and does not produce nested brackets, I did test on my local copy before submitting.

3) I didn't realise the source used tabs, my IDE auto converted them. I didn't see any problems with spaces in the other patches I could find but I'll remember if I ever dare to submit a patch again.

Note: See TracTickets for help on using tickets.