Modify support for new Block Editor settings with PHP

With 5.9, a lot of new features have come to the Block Editor. The main advertised way to modify these supports in any way is through the theme.json file. However, adding theme.json support comes with its own snarls that you may not want to deal with on an existing project, especially if it’s a complicated one. So I wanted to write a quick post on modifying Block Editor settings after this support ticket came in for the Super List Block.

Thankfully, there is a handy PHP filter you can hook into to enable or disable supports for Block Editor features as you please in your theme: block_editor_settings_all. You can find more information here in the Developer Resource page for the filter.

For example, let’s say you want to use the Super List Block and you know from the feature list and the tutorial, that the block supports both padding and margin. However, when you try out the block, you don’t see those settings in the Inspector (block sidebar)! As mentioned in the video tutorial, some settings are dependent on support being enabled in your theme, but with the help of block_editor_settings_all, you can enable support for the ones you’d like to use right away.

Example: enable padding and margin support

To enable support for Padding and Margin, you would add the following code to your functions.php file:

/**
 * Adds Dimension support to this theme
 *
 * @param array $editor_settings
 * @param object $editor_context
 * @return array $editor_settings
 */
function superlist_block_padding_margin_theme_support( $editor_settings, $editor_context ) {
    if ( ! empty( $editor_context->post ) ) {
        $editor_settings["enableCustomSpacing"] = true;
        $editor_settings["__experimentalFeatures"]["spacing"]["margin"] = true;

    }
    return $editor_settings;
}
 
add_filter( 'block_editor_settings_all', 'superlist_block_padding_margin_theme_support', 10, 2 );

Check the current list of settings

To see all the current settings available through the filter, you’ll find a helpful User Contributed Note from Hendrik that lists them, but you can always dump the $editor_settings array into your debug log to look at the latest version:

/**
 * Dump the contents of the $editor_settings into the log
 *
 * @param array $editor_settings
 * @param object $editor_context
 * @return array $editor_settings
 */
function aa_dump_editor_settings_in_log( $editor_settings, $editor_context ) {
    error_log( '<pre>' . var_export($editor_settings, true) . '</pre>' );
    return $editor_settings;
}
 
add_filter( 'block_editor_settings_all', 'aa_dump_editor_settings_in_log', 10, 2 );

Did you like this article? Get articles like this straight in your inbox for easy reading. 🙂