Stefan
Stefan

Reputation: 115

Create notice Gutenberg block editor CPT

For the Gutenberg block editor I want display an notice. With some research on the internet I create this JavaScript

wp.data.dispatch( 'core/notices' ).createNotice(
    'warning',
    'Please fill all filds',
    { 
        id: 'stFilAll', 
        isDismissible: true,
    }
);

I use the code below in my plugin file to enqueue the script

add_action("admin_enqueue_scripts", function(){ 
    wp_enqueue_script(
        "ownNotice_admin_script", 
        PLUGIN_URL."/assets/admin/js/test.js",
        '', 
        '1', 
    );
});

But does not showing up.

I put this code in the callback from the CPT but does not work. So I tried also to eneque the script and this give me an error "cant find variable wp"...

Who knows the answer to get this notice?

Upvotes: 1

Views: 80

Answers (1)

Emiel Zuurbier
Emiel Zuurbier

Reputation: 20944

Script that have to be enqueued to the block editor can be added through the enqueue_block_editor_assets hook. Since you're using the wp-data package, add it as a dependency.

add_action('enqueue_block_editor_assets', function() { 
  wp_enqueue_script(
    'ownNotice_admin_script', 
    PLUGIN_URL . '/assets/admin/js/test.js',
    ['wp-data'], 
    '1', 
  );
});

Upvotes: 0

Related Questions