3

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
( )

1 Answer 1

6

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}.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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