Special Offer: Get All 150 Premium WordPress Themes for Just $59.99 Purchase Now

contacts

Error with Blocks in Widgets Page in WordPress 5.8

WordPress 5.8 introduced using of Gutenberg blocks as widgets (Admin Panel -> Left Menu -> Appearance -> Widgets) as well. However, it may cause the following error:

wp_enqueue_script() was called incorrectly. "wp-editor" script should not be enqueued together with the new widgets editor

If you have such error, you may check the fix we applied:

Code which causes the issue:

function tishonator_register_blocks() {

// Skills Block
wp_register_script(
    plugins_url('js/skill.js', __FILE__),
    array( 'wp-editor',
        'wp-blocks',
        'wp-i18n',
        'wp-element', )
);

register_block_type( 'tishonator/skill-block', array(
                'editor_script' => 'tishonator-skill-block',
            ) );

}
add_action( 'init', 'tishonator_register_blocks' );

The problem was that wp-editor cannot be used when register a block on Widgets page (for editor the above works fine). So, we applied the following change: check if visitor is on Widgets page and update the dependency array based on that:

function tishonator_register_blocks() {

global $pagenow;

$arrDeps = ($pagenow === 'widgets.php') ?
                array( 'wp-edit-widgets', 'wp-blocks', 'wp-i18n', 'wp-element', )
              : array( 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', );

// Skills Block
wp_register_script(
                'tishonator-skill-block',
                plugins_url('js/skill.js', __FILE__),
                $arrDeps
            );

register_block_type( 'tishonator/skill-block', array(
                'editor_script' => 'tishonator-skill-block',
            ) );

}
add_action( 'init', 'tishonator_register_blocks' );