#30351 closed defect (bug) (duplicate)
Input field with HTML tag as default value
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.0 |
Component: | Widgets | Keywords: | reporter-feedback |
Focuses: | ui, javascript, administration | Cc: |
Description (last modified by )
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)
Change History (13)
#2
@
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
@
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:
↓ 5
@
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
@
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
@
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
Please attach an example widget which demonstrates the issue via code. Thanks!