Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#26355 closed defect (bug) (duplicate)

has_shortcode does not find nested shortcodes

Reported by: bseddon's profile bseddon Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.6
Component: Shortcodes Keywords:
Focuses: Cc:

Description

If a shortcode is nested within another shortcode, say a video tag inside an accordion tag, has_shortcode will report false when asked if content contains the inner video tag.

A solution is to call has_shortcode recursively if parsed tag contains text. The modification to the existing has_shortcode is simple.

Add these two lines after the conditional test in the has_shortcode function:

if ($shortcode[5] && has_shortcode($shortcode[5], $tag))
        return true;

The first part of the first line confirms the parsed tag has content and, if so, the function is called recursively. If the nested call return 'true' then true is returned.

The full function then looks like:

function has_shortcode( $content, $tag ) {
        if ( shortcode_exists( $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;

                        if ($shortcode[5] && has_shortcode($shortcode[5], $tag))
                                return true;
                }
        }
        return false;
}

Bill Seddon

Change History (1)

#1 @ocean90
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #26343.

Note: See TracTickets for help on using tickets.