Check if a post has one of the following shortcodes, then enqueue scripts and styles

Question

I want to check if a post has one of the following shortcodes, wpdocs-shortcode-1, wpdocs-shortcode-2, wpdocs-shortcode-3. If it found any of those shortcodes, then do enqueue scripts and styles.

Basically this code works.

    function wpdocs_shortcode_scripts() {
      global $post;
      if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpdocs-shortcode') ) {
       wp_enqueue_script( 'wpdocs-script');
      }
   }
   add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts');

What I’d like to achieve is check for more than one shortcodes. I tried the following code by passing an array but it did not work.

 function wpdocs_shortcode_scripts() {
  global $post;
  if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, array('wpdocs-shortcode-1', 'wpdocs-shortcode-2', 'wpdocs-shortcode-3') ) {
   wp_enqueue_script( 'wpdocs-script');
  }
 }
add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts');

Any help would be greatly appreciated.

Thank you so much!

0
, battx726 3 years 2020-07-17T08:10:32-05:00 0 Answers 63 views 0

Leave an answer

Browse
Browse