How to import the imagesLoaded and Masonry libs that come with WP in a Gutenberg block?

When developing a block with @wordpress/scripts, wp-scripts uses @wordpress/dependency-extraction-webpack-plugin to allow you to import dependencies from WordPress by replacing the import statements with references to the global variables loaded by the enqueued scripts. For example, this: import { useEffect } from ‘@wordpress/element’ Will become: const { useEffect } = wp.element; And wp-element will be added … Read more

How to change the selected Template using javascript?

You are already using the correct action, i.e. wp.data.dispatch( ‘core/editor’ ).editPost(), however, you should instead change the property named template. I.e. wp.data.dispatch( ‘core/editor’ ).editPost( { template: ‘article.php’ } ) Yes, _wp_page_template is the meta key in the database, but in the REST API, a property named template exists in place of direct access on that … Read more

How to use webpack in WordPress theme? I want some scripts to load in the footer, some in the header and some with script parameters

What is the right and clean way to load these files in WordPress? The same way as you’ve always done it, wp_enqueue_script. WordPress is unaware that webpack created the javascript file, and it is just that, a javascript file. This is true wether it contains jquery, was written by hand, or was created by a … Read more

Processing javascript on wordpress

The script tags are only there to contain the JavaScript that will be executed by the browser. You need to tell WordPress to ignore those tags so it doesn’t process them as HTML. One way to do this is to use the “esc_html” function. You’ll need to wrap the script tags in the following code: … Read more