[wp-trac] [WordPress Trac] #54958: Inconsistent behaviour for wp_add_inline_script between block-based and standard themes

WordPress Trac noreply at wordpress.org
Thu Jan 27 17:31:11 UTC 2022


#54958: Inconsistent behaviour for wp_add_inline_script between block-based and
standard themes
--------------------------+-----------------------------
 Reporter:  Rustaurius    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When using a non-block based theme, data can be passed from PHP to JS
 inside of a shortcode using wp_add_inline_script:

 Main plugin file
 {{{
 add_action( 'wp_enqueue_scripts', 'plugin_register_assets' );
 function plugin_register_assets() {
     wp_register_script( 'plugin-js-handle', PLUGIN_URL .
 '/assets/js/file.js', array( 'jquery' ), PLUGIN_VERSION, true );
 }
 }}}

 Shortcode

 {{{
 class shortcodeClass {
 public function render() {
     $this->enqueue_assets();

     // Output shortcode content
 }

 public function enqueue_assets() {
     $args = array(
         // array data here
     );

     wp_enqueue_script( 'plugin-js-handle' );

     wp_add_inline_script(
         'plugin-js-handle',
         'const plugin_php_data = ' . json_encode( $args ),
         'before'
     );
 }
 }
 }}}

 When using a block theme, the following JS error is generated:
 Uncaught ReferenceError: plugin_php_data is not defined

 Specifically, the JS file handle isn’t being evaluated as registered on
 line 288 of class.wp-dependencies.php, and so wp_add_inline_script is
 failing, but only when a block-base theme is in use (I tested with the
 Twenty Twenty-Two and Tove block themes, works with all of the default,
 etc. non-block-based themes).

 Is this an intended change in behaviour?

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


More information about the wp-trac mailing list