Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/block-editor getSettings() does not return custom settings added from filter #68541

Open
3 of 6 tasks
colinduwe opened this issue Jan 7, 2025 · 4 comments
Open
3 of 6 tasks
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Block editor /packages/block-editor [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@colinduwe
Copy link
Contributor

Description

According to https://make.wordpress.org/core/2022/05/03/block-editor-miscellaneous-dev-notes-for-wordpress-6-0/#removed-6-2, wp.data.select( 'core/editor' ).+ name has been deprecated in favor of wp.data.select( 'core/block-editor' ). + name.

When using the filter block_editor_settings_all, additional settings aren't always included in core/block-editor, for example when editing a page.

This is blocking
#68538
#17854
#56831

There is an old, closed issue but it doesn't seem to have been addressed

Step-by-step reproduction instructions

add_filter('block_editor_settings_all', function (array $settings): array {
  $settings['customAttribute'] = ['one', 'two', 'three'];
  return $settings;
});

Then on post.php

wp.data.select('core/editor').getEditorSettings().customAttribute
// Array(3) [ "one", "two", "three" ]

wp.data.select('core/block-editor').getSettings().customAttribute
// undefined

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 6.7.1

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

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@colinduwe colinduwe added the [Type] Bug An existing feature does not function as intended label Jan 7, 2025
@Mamaduka Mamaduka added [Package] Block editor /packages/block-editor Needs Technical Feedback Needs testing from a developer perspective. labels Jan 8, 2025
@Infinite-Null
Copy link
Contributor

Infinite-Null commented Jan 8, 2025

Hi @colinduwe,
Thank you for bringing this up. I was successfully able to reproduce it by following these steps:

  1. Created a plugin and added following code:
add_filter('block_editor_settings_all', function (array $settings): array {
  $settings['customAttribute'] = ['one', 'two', 'three'];
  return $settings;
});
  1. Go to post or page editor so that WordPress JavaScript APIs are loaded and pasted following into console:
wp.data.select('core/editor').getEditorSettings().customAttribute
wp.data.select('core/block-editor').getSettings().customAttribute

Screenshot:

Image

@Mamaduka
Copy link
Member

Mamaduka commented Jan 8, 2025

If I remember correctly, this is intentional, and even Core doesn't expose new settings via block_editor_settings_all. It's recommended that settings be exposed and consumed via REST API. Here's the latest example - #61844 (comment).

Additional ref: https://make.wordpress.org/core/2024/09/12/gutenberg-development-practices-and-common-pitfalls/#:~:text=avoid%20over%20time%3A-,Avoid%20passing%20editor%20settings,-as%20inline%20JS

@colinduwe, can you share more about your use case? Let's see if we can find an alternative.

@colinduwe
Copy link
Contributor Author

We need an extensible way to set the body class on the editor iframe. Here are two issues
#56831
#17854

Here's the component with its comment that alludes to this unmet need:

// Ideally ALL classes that are added through get_body_class should
// be added in the editor too, which we'll somehow have to get from
// the server in the future (which will run the PHP filters).
setBodyClasses(
Array.from( ownerDocument.body.classList ).filter(
( name ) =>
name.startsWith( 'admin-color-' ) ||
name.startsWith( 'post-type-' ) ||
name === 'wp-embed-responsive'

Here's a proposed way to provide for the needs (rudimentary): #68538

If we can add a bodyClasses property to $settings which is available via the 'core/block-editor' store, then extenders can use the PHP filter or modify the editor settings state to control the body classes and thus target CSS.

My personal motivating use case is a theme with page templates with different background colors. The page template class should be on iframe body so it can have the proper background color. This was something we had in TinyMCE.

@colinduwe
Copy link
Contributor Author

If there's an alternative method for providing a bodyClasses property to the core/block-editor store, maybe we should close this issue and update the documentation for block_editor_settings_all so it clearly states that the filter only works to modify existing properties and cannot be used to add properties along with information about how to add properties to the editor settings today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Block editor /packages/block-editor [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants