Widget Update Code Not Working

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.

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

Related posts

Leave a Reply

1 comment

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