Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#30351 closed defect (bug) (duplicate)

Input field with HTML tag as default value

Reported by: gdragon's profile GDragoN Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: Widgets Keywords: reporter-feedback
Focuses: ui, javascript, administration Cc:

Description (last modified by SergeyBiryukov)

If you create a widget with INPUT field, and if default value for that field is a HTML tag, when adding this widget into sidebar, __i__ for this INPUT is not replaced by sequential number for the widget. If the default value is not tag, it works fine. If after adding widget to sidebar you set HTML tag as value it is saved OK also. Problem is when field has HTML as default value.

From what I can see, this is a bug that is there since the Widget class is added to WordPress years ago. Problem is the regular expression that detects __i__ inside the HTML generated for the widget control.

Milan

Attachments (4)

example_widget_class.txt (1.2 KB) - added by GDragoN 10 years ago.
widget-added-screenshot.png (35.2 KB) - added by westonruter 10 years ago.
example_widget_class_full.txt (1.4 KB) - added by GDragoN 10 years ago.
foo_widget.php (2.6 KB) - added by valendesigns 10 years ago.

Download all attachments as: .zip

Change History (13)

#1 @westonruter
10 years ago

  • Keywords reporter-feedback added

Please attach an example widget which demonstrates the issue via code. Thanks!

#2 @GDragoN
10 years ago

I have added example_widget_class.txt file, with just basic form() method. Here you have field 'message' with default value set to use content with HTML tag. When such widget is first add to the sidebar, field message will display this default value, but when saved that will be gone, because when added to sidebar i in the name for the message field is not replaced by actual widget number because value contains HTML.

Milan

#3 @westonruter
10 years ago

I don't seem to see what you're seeing. I added the widget and saved it and re-saved it, and the message remained the default value as shown in the above screenshot.

#4 follow-up: @GDragoN
10 years ago

My bad. Example was missing the code to save the input values, so it always uses defaults. I uploaded example_widget_class_full.txt with saving of settings.

I have tracked the problem to widgets.js in wp-admin/js, line 159 (version 4.0) where the tags are detected.

Milan

#5 in reply to: ↑ 4 @adamsilverstein
10 years ago

Replying to GDragoN:

My bad. Example was missing the code to save the input values, so it always uses defaults. I uploaded example_widget_class_full.txt with saving of settings.

I have tracked the problem to widgets.js in wp-admin/js, line 159 (version 4.0) where the tags are detected.

Milan

Hi Milan!

Trying to test this, I can't get the widget to load at all - can you give some details on how to get this set up, or a more complete example? Also, looks like a missing open single quote in the code - http://cl.ly/image/112D3f3f072B

Thanks.

#6 @valendesigns
10 years ago

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

I was able to create a widget that works correctly by building off the example code from http://codex.wordpress.org/Widgets_API. As well, I sanitized message, because the way you had it would allow unwanted code injections.

I believe the underlying issue was the way you wrote your form method which does not allow the default value to be saved, because the value of message was always an empty string. I didn't want to take too much time hunting down the exact reason when there is an obvious solution, but it has something to do with wp_parse_args and the order of your arrays. If you change it to use $instance = wp_parse_args( $defaults, $instance ); or $instance = array_merge( $instance, $defaults ); everything works. However, the first fix feels wrong, and the second fix will work, but there are other ways to accomplish your goals.

I don't believe this is an issue with the Core as much as how the code is written to save/display the form values.

Cheers,
Derek

#7 @ocean90
10 years ago

  • Milestone Awaiting Review deleted

#8 @SergeyBiryukov
10 years ago

  • Description modified (diff)
  • Resolution changed from worksforme to duplicate

Duplicate of #18446.

#9 @SergeyBiryukov
10 years ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.