Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

From what I can tell the widget API is pretty straight forward and simple, yet for some reason my update code isn't taking effect properly. When I press [save] from the backend all fields are blanked, and nothing is actually saved.

When I drag my widget onto the sidebar region, an entry is created in the database for the widget.

1846, 0, 'widget_email-signup', 'a:2:{i:2;a:0:{}s:12:"_multiwidget";i:1;}', 'yes'

When I input values and save, none are saved. I've intercepted both $new_instance and $old_instance during the call to my update method and wrote their outputs to a file, but found nothing in them.

Full Code: http://pastebin.com/gtrfuciZ

Update Method

function update( $new_instance, $old_instance ) {
  $instance = $old_instance;

  $instance['title'] = $new_instance['title'];
  $instance['script'] = $new_instance['script'];

  file_put_contents( '/wp_results.txt', print_r( $old_instance, true ) );
  file_put_contents( '/wp_results.txt', print_r( $new_instance, true ), FILE_APPEND );

  return $instance; 
}

wp_results.txt (data found within update method)

Array
(
    [title] => 
    [script] => 
)
Array
( )
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

You're using get_field_id() for the name attribute of the form elements, but you should use get_field_name().

The field names are of the form widget-{$id_base}[{$number}][{$field_name}], but id's can't use [], so they are like widget-{$id_base}-{$number}-{$field_name}.

share|improve this answer
    
get_field_name for the field name. Example code is in codex: codex.wordpress.org/Widgets_API#Example –  hakre Dec 29 '10 at 2:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.