How to check if a post has any one of many shortcodes?

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 1 year 2020-07-17T12:10:25-05:00 0 Answers 60 views 0

Leave an answer

Browse
Browse