gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Site Editor: gradient and duotone controls do not honor values of `wp_theme_json_data_user` filter

Open webmandesign opened this issue 2 years ago • 7 comments

Description

In my theme I am automatically producing gradients and duotones based on (user customized) theme palette colors. For this I am using wp_theme_json_data_theme and wp_theme_json_data_user filter hook.

Everything is working perfectly fine in block editor when editing a page/post.

However, I've found out the gradient and duotone controls in Site Editor take values from wp_theme_json_data_theme filter only - the wp_theme_json_data_user does not seem to be honored for the controls.


Expected behavior:

As the wp_theme_json_data_user filter is called after the wp_theme_json_data_theme filter, the gradient and duotone controls should display values set in wp_theme_json_data_user filter.

Step-by-step reproduction instructions

  1. In Twenty Twenty-Four theme open functions.php file and add this PHP code at the end:
// This works in Site Editor:
add_filter( 'wp_theme_json_data_theme', function( $theme_json ) {
	$data = $theme_json->get_data();
	$data['settings']['color']['gradients']['theme'] = [
		[
			'name'     => 'JSON Theme',
			'slug'     => 'json-theme',
			'gradient' => 'linear-gradient( 0deg, #000000, #ffffff )', // Black & white gradient.
		],
	];
	return $theme_json->update_with( $data );
} );

// This does not work in Site Editor:
add_filter( 'wp_theme_json_data_user', function( $theme_json ) {
	$data = $theme_json->get_data();
	$data['settings']['color']['gradients']['theme'] = [
		[
			'name'     => 'JSON User',
			'slug'     => 'json-user',
			'gradient' => 'linear-gradient( 0deg, #ff0000, #0000ff )', // Red & blue gradient.
		],
	];
	return $theme_json->update_with( $data );
} );
  1. Navigate to Site Editor and either open a template or template part for editing or create a new one.
  2. Select a block where you can set a background color/gradient and try to select a gradient for the block.
  3. You should see there is a single black & white gradient, which was added via wp_theme_json_data_theme filter in our PHP code. The red & blue gradient added via wp_theme_json_data_user filter does not work. As the wp_theme_json_data_user filter is called after the wp_theme_json_data_theme filter, you should see the red & blue gradient instead.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.4.2
  • Gutenberg 17.2.0

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

webmandesign avatar Dec 09 '23 15:12 webmandesign

Some more debug info: It seems to be related to data passed in block_editor_rest_api_preload() function in this code:

	wp_add_inline_script(
		'wp-api-fetch',
		sprintf(
			'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
			wp_json_encode( $preload_data )
		),
		'after'
	);

webmandesign avatar Dec 09 '23 15:12 webmandesign

Hi, This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps. Thanks for helping out.

github-actions[bot] avatar Jan 10 '24 00:01 github-actions[bot]

The issue is still occurring on WP6.4 + Gutenberg 17.4.1

webmandesign avatar Jan 10 '24 18:01 webmandesign

Hi, This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps. Thanks for helping out.

github-actions[bot] avatar Feb 11 '24 00:02 github-actions[bot]

The issue is still occurring on WP6.4.3 + Gutenberg 17.7.0

webmandesign avatar Feb 21 '24 13:02 webmandesign

I was able to reproduce this bug on the latest environment. My environment details are as follows:

  • WordPress: 6.9-alpha-60275
  • Gutenberg: 21.2.0-rc.1
  • Theme: Twenty Twenty-Five 1.2

I used the provided code snippet to filter the settings and add an additional theme gradient. Below are the results:

Template Editor

Image

Post/Page Editor

Image|

BugReportOnWeb avatar Jul 10 '25 10:07 BugReportOnWeb

It seems we already got a successful reproduction report by @BugReportOnWeb. Time to remove Needs Testing tag.

SirLouen avatar Jul 31 '25 15:07 SirLouen