<?php
/**
* Our theme/plugin enqueue scripts function.
*/
function example_enqueue_scripts() {
// Register the script in the normal WordPress way.
wp_register_script( 'example-shortcode-js', '...example-shortcode-script.js' );
// filemtime( dirname( __FILE__ )
// Grab the global $post object.
global $post;
// See if the post HAS content and, if so, see if it has our shorcode.
if ( isset( $post->post_content ) && has_shortcode( $post->post_content, 'example_shortcode' ) ) {
wp_enqueue_script( 'example-shortcode-js' );
}
}
add_action( 'wp_enqueue_scripts', 'example_enqueue_scripts' );
/**
* Our Custom Shortcode.
*/
function example_shortcode_function(){
return 'Hello World!';
}
add_shortcode( 'example_shortcode', 'example_shortcode_function' );https://generatepress.com/forums/topic/enqueue-scripts-when-shortcode-is-present-inside-a-block-element/