Skip to content

Instantly share code, notes, and snippets.

@colorful-tones
Last active December 6, 2023 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colorful-tones/19fe68f23cb79b750f7791d8a5516e9c to your computer and use it in GitHub Desktop.
Save colorful-tones/19fe68f23cb79b750f7791d8a5516e9c to your computer and use it in GitHub Desktop.
Load JavaScript once on the page if a core block exists. As adapted from https://wordpress.stackexchange.com/a/328553
function enqueue_my_awesome_script_if_there_is_block( $content = '' ) {
if ( has_block( 'core/list' ) ) {
wp_enqueue_script(
'my-awesome-script',
plugins_url( 'hello-world.js', __FILE__ ),
array(),
'0.1.1',
true
);
}
return $content;
}
add_filter( 'the_content', 'enqueue_my_awesome_script_if_there_is_block' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment