AdvancedCustomFields / AdvancedCustomFields/acf

ACF field values not loaded correct in new widget block editor

Open
#611 7 comments 0 reactions 1 assignee View on GitHub

@lgladdy is already working on this.

Since Feb 17, 2022.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Description

In the new widget block editor we have the problem that if ACF fields are used in the widget form the field values of the wrong instance might be loaded.

Setup

Tested with a clean install of WP 5.9, Theme: Twenty Twenty-One and ACF 5.11.4 and also ACF 5.12-RC1

Scenario.
  1. Create a custom widget by extending the WP_Widget class. (https://developer.wordpress.org/themes/functionality/widgets/)
  2. Add an ACF field to the widget form. (i.e. a text field)
  3. Insert the custom widget in a sidebar and insert text ‘first widget’.
  4. Add another instance of this custom widget after the first instance in the sidebar and insert text ‘second widget’.
  5. Save the sidebar.
  6. Refresh the page
  7. Now switch positions of the widgets, so the widget named second widget is in the 1st place of appearance
  8. Save the sidebar
  9. Refresh te page
  10. Notice the content of the widgets. The 1st instance has ‘first widget’ again and the 2nd instance has ’second widget’ again
My findings

The new Gutenberg editor passes a number in the async request which loads the field values for the widget instance. This number seems like a simple increment of the position the widget is in. But this number is treated like it is the unique ID for the widget. This is why the data is getting mixed up.

Code

This is just for purpose of simple and quick prototyping.

Functions.php

Add to functions.php of your theme
require_once ('widget.php'); add_action( 'widgets_init', function () { register_widget('TestWidget'); } );

widget.php

Create a new widget.php in the theme folder (same where your functions.php lives)


/**
 * @psalm-suppress PropertyNotSetInConstructor
 */
class TestWidget extends WP_Widget {

	public function __construct() {

		$widget_ops = array(
			'classname'   => 'test-widget',
			'description' => __( 'Test Widget', 'test' ),
			'show_instance_in_rest' => true,
		);
		parent::__construct( 'test_widget', __( 'Test Widget', 'test' ), $widget_ops );
	}

	public function update( $new_instance, $old_instance ) {
		return parent::update( $new_instance, $old_instance );
	}

	public function form( $instance ):array {
		return $instance;
	}

	public function widget( $args, $instance ):void {
		echo get_field( 'test_text', 'widget_' . $args['widget_id'] );
	}
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.