php - Enque a javascript when a shortocode is call in wordpress -


i want call/add/enque javascript file when shortcode executed.

below code, please let me know wrong doing.

add_shortcode('footprint', 'do_footprint');  function do_footprint() {        add_action( 'wp_enqueue_scripts', 'pace_enque_map_scripts' );      ob_start();     ?>      <div id="map"></div>      <?php     return ob_get_clean(); }  function pace_enque_map_scripts() {     wp_enqueue_script( 'my-js', 'filename.js', false ); } 

when call action hook add javascript directly works. when call action hook in function doesn't works.

please guide

you can this,

function do_footprint() {      ob_start(); ?>     <div id="map"></div>     <?php     return ob_get_clean(); } add_shortcode('footprint', 'do_footprint');  function pace_enque_map_scripts() {     global $post;     //check shortcode existence in post content , enque script if found     if( is_a( $post, 'wp_post' ) && has_shortcode( $post->post_content, 'footprint') && !is_admin() ) {         wp_enqueue_script( 'my-js', 'filename.js', false );     } } add_action( 'wp_enqueue_scripts', 'pace_enque_map_scripts'); 

Comments