Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
Got a custom block and need to enqueue its styles and scripts? The enqueue_block_assets action is your go-to! Just make sure to hook into it before wp_enqueue_scripts. It’s super handy for developers like us who prefer creating custom plugins to avoid breaking themes during updates.
Here’s a quick rundown: first, register the action using add_action. You can drop this code into your theme’s functions.php or, better yet, in a custom plugin. Below, you’ll find live examples of how to use this action effectively.
Want to enqueue specific styles and scripts for your blocks? This example shows you how.
function weplugins_enqueue_block_styles() { wp_enqueue_style( 'testimonial', get_stylesheet_directory_uri() . '/template-parts/blocks/testimonial/testimonial.css' ); wp_enqueue_script( 'testimonial', get_stylesheet_directory_uri() . '/template-parts/blocks/testimonial/testimonial.js' ); } add_action( 'enqueue_block_assets', 'weplugins_enqueue_block_styles' );Live Example 2: Basic Hook Execution
Just need to execute some code when the enqueue_block_assets action fires? Here’s a simple example.
function weplugins_execute_on_enqueue_block_assets_event(){ // Your custom code goes here. } // Add the action add_action( "enqueue_block_assets", "weplugins_execute_on_enqueue_block_assets_event");Live Example 3: Removing a Registered Hook
If you need to remove a previously registered hook, use the example below. Just make sure to provide the same callback function name, priority, and number of arguments.
remove_action( "enqueue_block_assets", "weplugins_execute_on_enqueue_block_assets_event");If you’re having any trouble using this hook, feel free to Contact Us for customization. Our team at WePlugins is always ready to assist!
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.