admin管理员组

文章数量:1125904

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!

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!

Share Improve this question edited Feb 2, 2024 at 20:06 alo Malbarez 4451 gold badge6 silver badges7 bronze badges asked Jul 17, 2020 at 13:09 battx726battx726 233 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

So yes, as you say you can't pass an array to has_shortcode, which is confirmed in the docs, so you need to do the 'OR' operation manually, which would look something like this:

$shortCodesToTest = [ 'wpdocs-shortcode-1', 'wpdocs-shortcode-2', 'wpdocs-shortcode-3'];

$anyShortCode = false;

foreach ($shortCodesToTest as $sc) {
    if (has_shortcode( $post->post_content, $sc) ) {
        $anyShortCode = true;
    }
}

Now use the $anyShortCode variable as you like - it will be True if any of the shortcodes were found. For example:

if ( is_a( $post, 'WP_Post' ) && $anyShortCode ) {
    wp_enqueue_script( 'wpdocs-script');
}

You can loop through the shortcuts and then put something into an array when you get a hit.

If the array is not empty at the end, your script can be loaded.

<?php
function wpdocs_shortcode_scripts() {
    global $post;

    $shortcodes = [ 'wpdocs-shortcode-1', 'wpdocs-shortcode-2', 'wpdocs-shortcode-3' ];

    $found = [];

    foreach ( $shortcodes as $shortcode ) :
        if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $shortcode ) ) {
            $found[] = $shortcode;
        }
    endforeach;

    if ( ! empty( $found ) ) {
        wp_enqueue_script( 'wpdocs-script' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts' );

Another take using get_shortcode_regex(array $tagnames = null ): string that allows for múltiple tags in one check.

as a variation of has_shortcode( $content, $tag ) source.

function wpdocs_shortcode_scripts() {
  global $post;
  if (
    is_a($post, 'WP_Post')
    && !empty($post->post_content)
  ) {
    $tagnames= [
      'wpdocs-shortcode-1',
      'wpdocs-shortcode-2',
      'wpdocs-shortcode-3'
    ];
    $pattern = get_shortcode_regex($tagnames);
    if (
      preg_match_all(
        "/$pattern/",
        $post->post_content,
        $matches,
        PREG_SET_ORDER
      )
    ) {
      wp_enqueue_script('wpdocs-script');
    }
  }
}

add_action('wp_enqueue_scripts', 'wpdocs_shortcode_scripts');

[*] the $matches array will contain all the found shortcodes with their attributes in the form described at do_shortcode_tag( $m ) source.

379  * @param array $m {
380  *     Regular expression match array.
381  *
382  *     @type string $0 Entire matched shortcode text.
383  *     @type string $1 Optional second opening bracket for escaping shortcodes.
384  *     @type string $2 Shortcode name.
385  *     @type string $3 Shortcode arguments list.
386  *     @type string $4 Optional self closing slash.
387  *     @type string $5 Content of a shortcode when it wraps some content.
388  *     @type string $6 Optional second closing brocket for escaping shortcodes.
389  * }

To refine the filter by tag and/or attribute within that list you can do something like:

$tagnames= [
 'wpdocs-shortcode-1',
 'wpdocs-shortcode-2',
 'wpdocs-shortcode-3'
];
$pattern = get_shortcode_regex($tagnames);
if (
  preg_match_all(
    "/$pattern/",
    $post->post_content,
    $matches,
    PREG_SET_ORDER
  )
) {
  // theone for all tags in the list
  wp_enqueue_script('wpdocs-script');

  // find and add something for [wpdocs-shortcode-2 extra="something"]

  foreach ( $matches as $shortcode ) {
    $tag = $shortcode[2];
    $atts = shortcode_parse_atts($shortcode[3]);
    if (
      'wpdocs-shortcode-2' == $tag
      && 'something' == atts['extra'] ?? ''
    ) {
      // the extra one for that particular case
      wp_enqueue_script('wpdocs-script-2-extra-something');
    }
  }
}

本文标签: