javascript - How to check shortcode with variables using has_shortcode() in WordPress -
i'm trying following approach enqueue css/js files within plugin when shortcode being used:
add_action( 'wp_enqueue_scripts', function() { global $post; if( is_a( $post, 'wp_post' ) && has_shortcode( $post->post_content, 'shortcode') ) { //add css/js here } } );
now problem i'm having code if use shortcode [shortcode]
works fine when pass variable within shortcode [shortcode id="1" class="something"]
cannot detect , hence doesn't add css/js files.
can bit?
you can try code,
add_action( 'wp_enqueue_scripts', function( $id, $class) { global $post; if( is_a( $post, 'wp_post' ) && has_shortcode( $post->post_content, 'shortcode') ) { //enqueue css/js files here } }
then use shortcode: [shortcode id="1" class="something"]
Comments
Post a Comment