wp_add_inline_script with block-based themes
-
Hello!
I’ve got a question about using wp_add_inline_script to pass PHP data to a JS file. Are there new restrictions on how that can be done when using a block-based theme? I’ve got some code that was working previously (outlined below), that appears to not work with any block-based theme.
Specifically, the JS 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).
Here are the stripped out portions of code that isn’t working. If there are any other sections that would be helpful to see, please let me know.
In the main plugin file:
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) ); public function register_assets() { wp_register_script( 'ewd-ufaq-js', EWD_UFAQ_PLUGIN_URL . '/assets/js/ewd-ufaq.js', array( 'jquery', 'jquery-ui-core' ), EWD_UFAQ_VERSION, true ); }
Inside of the shortcode file:
public function render() { $this->enqueue_assets(); } public function enqueue_assets() { $args = array( // array data here ); wp_enqueue_script( 'ewd-ufaq-js' ); wp_add_inline_script( 'ewd-ufaq-js', 'const ewd_ufaq_php_data = ' . json_encode( apply_filters( 'ewd_ufaq_js_localize_data', $args ) ), 'before' ); }
Again, works the code seems to work perfectly in non-block based themes but isn’t printed at all in the two block-based themes that I tested with. Thanks for any help as to what I might be doing wrong!
- The topic ‘wp_add_inline_script with block-based themes’ is closed to new replies.