Users can select an item from the list (the items are actually listed inside a DIV container). Upon selection, the item is added below with a leading checkbox in checked mode. When saved, the items get saved.
This flow works fine when I activate the Classic Widget editor.
But with the Block Widget editor, the checkbox values are simply getting ignored. I am not getting them inside $new_instance
.
Here is the code written inside the update() method of the Widget:
function update( $new_instance, $old_instance ) {
$instance = $new_instance
....
....
// Unfortunately, these don't come through $new_instance
$instance['groups'] = is_array($_POST['groups']) ? array_values($_POST['groups']) : array();
}
I am never getting any value inside $instance['groups']
.
I added an additional textbox (<input type="hidden" name="instance-selected-groups" class="instance-selected-groups" value="' . $instance['instance-selected-groups'] . '" />
) where I am storing comma separated item values using JavaScript like this.
I tried to grab these values in the update method like this:
if( empty( $instance['groups'] ) ) {
if( strlen( trim( $_POST['instance-selected-groups'] ) ) > 0 ) {
$instance['groups'] = array_pop( explode( ',', $_POST['instance-selected-groups'] ) );
var_dump( $instance['groups'] );
}
}
This doesn't work either.
$_POST
in a widget's update method. You should be using$new_instance
and your inputs need a proper dynamic name for that to work. You should review the documentation for developing widgets as it seems like your approach is just incorrect: developer.wordpress.org/themes/functionality/widgets