[wp-trac] [WordPress Trac] #57421: Editor: add new parameter to hook when enqueuing block assets

WordPress Trac noreply at wordpress.org
Thu Jan 5 10:12:09 UTC 2023


#57421: Editor: add new parameter to hook when enqueuing block assets
--------------------------+-----------------------------
 Reporter:  jeherve       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:  5.8
 Severity:  normal        |   Keywords:  has-patch
  Focuses:  javascript    |
--------------------------+-----------------------------
 When enqueuing assets for a block registered in a plugin, one can rely on
 `block.json`, or hook into the `enqueue_block_editor_assets` hook to
 enqueue necessary scripts and styles in the block editor.

 This works well, but offers some challenges with some specific blocks. For
 instance, your block may rely on the `@wordpress/editor` dependency to
 handle some of its functionality. That dependency is available in the post
 editor, but cannot be used in the widget editor or the site editor. See
 #53569 and [https://github.com/WordPress/gutenberg/issues/33203
 33203-gutenberg] for more information on this topic.

 In this scenario, one may want to enqueue the block's scripts (and its
 `wp-editor` dependency) **only in the post editor**.

 The `enqueue_block_editor_assets` hook doesn't currently offer this
 flexibility. As a result, folks wanting to do that must add a check before
 they enqueue. Something like this for someone not wanting to load in the
 widget editor for example.

 {{{#!php
 <?php
 function enqueue_block_assets() {
     global $pagenow;

     // Return early when viewing the customizer or widgets screen.
     if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
         return;
     }

     wp_enqueue_script( 'here' );
 }
 }}}


 I was consequently wondering if it were possible to extend the
 `enqueue_block_editor_assets` hook a bit, so it could be used just like
 the `admin_enqueue_scripts` hook today, so we could do something like
 this:

 {{{#!php
 <?php
 function enqueue_block_assets( $hook ) {
     // We only need our script in the post editor
     // not in the widget editor or the site editor.
     if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) )
 {
         return;
     }

     wp_enqueue_script( 'here' );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57421>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list