WordPress.org

Ready to get started?Download WordPress

Ideas

Allow array of shortcodes in has_shortcode

  1. Anna Johansson
    Member

    12345

    The has_shortcode( $content, $tag ) second argument ($tag) only allows for a string so checking for multiple shortcodes "in one go" isn't possible right now. It would be helpful to be able to put an array of shortcodes to check for like this:

    function my_has_shortcode( $content, $tags ) {
    
        if( is_array( $tags ) ) {
            foreach ( $tags as $tag ) {
                preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
                if ( empty( $matches ) )
                        return false;
                foreach ( $matches as $shortcode ) {
                        if ( $tag === $shortcode[2] )
                            return true;
                }
            }
        } else {
            if ( shortcode_exists( $tags ) ) {
                preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
                if ( empty( $matches ) )
                        return false;
                foreach ( $matches as $shortcode ) {
                        if ( $tags === $shortcode[2] )
                                return true;
                }
            }
        }
    
        return false;
    
    }
    Posted: 7 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    5 Votes
  • Status

    This idea is under consideration