Using WordPress ‘get_legacy_widget_block_editor_settings()’ PHP function

The get_legacy_widget_block_editor_settings WordPress PHP function returns the block editor settings needed to use the Legacy Widget block, which is not registered by default.

On this pageJump to a section

Usage

To use the function, call it like this:

$legacy_widget_settings = get_legacy_widget_block_editor_settings();

Parameters

This function does not have any parameters.

More information

See WordPress Developer Resources: get_legacy_widget_block_editor_settings

Examples

Enabling the Legacy Widget block

Enable the Legacy Widget block by adding the settings to the block editor.

// Get the legacy widget block editor settings.
$legacy_widget_settings = get_legacy_widget_block_editor_settings();

// Add the legacy widget settings to the block editor.
add_filter('block_editor_settings', function ($editor_settings) use ($legacy_widget_settings) {
    return array_merge($editor_settings, $legacy_widget_settings);
});

Displaying the Legacy Widget block settings

Print the settings needed for the Legacy Widget block on the page.

// Get the legacy widget block editor settings.
$legacy_widget_settings = get_legacy_widget_block_editor_settings();

// Print the settings array.
echo '<pre>';
print_r($legacy_widget_settings);
echo '</pre>';

Checking if Legacy Widget block is enabled

Determine if the Legacy Widget block is enabled in the block editor settings.

// Get the current block editor settings.
$block_editor_settings = get_option('block_editor_settings');

// Get the legacy widget block editor settings.
$legacy_widget_settings = get_legacy_widget_block_editor_settings();

// Check if the legacy widget block is enabled.
$is_legacy_widget_enabled = array_intersect_key($block_editor_settings, $legacy_widget_settings) === $legacy_widget_settings;

// Print the result.
echo $is_legacy_widget_enabled ? 'Legacy Widget is enabled' : 'Legacy Widget is not enabled';

Adding a custom setting to the Legacy Widget block

Add a custom setting to the Legacy Widget block settings.

// Get the legacy widget block editor settings.
$legacy_widget_settings = get_legacy_widget_block_editor_settings();

// Add a custom setting.
$legacy_widget_settings['custom_setting'] = 'Custom value';

// Update the legacy widget block settings.
add_filter('block_editor_settings', function ($editor_settings) use ($legacy_widget_settings) {
    return array_merge($editor_settings, $legacy_widget_settings);
});

Removing a specific setting from the Legacy Widget block

Remove a specific setting from the Legacy Widget block settings.

// Get the legacy widget block editor settings.
$legacy_widget_settings = get_legacy_widget_block_editor_settings();

// Remove a specific setting.
unset($legacy_widget_settings['setting_to_remove']);

// Update the legacy widget block settings.
add_filter('block_editor_settings', function ($editor_settings) use ($legacy_widget_settings) {
    return array_merge($editor_settings, $legacy_widget_settings);
});

Leave a Comment

Your email address will not be published. Required fields are marked *