WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#29029 closed enhancement (wontfix)

get_shortcodes() - retrieve the shortcode instances from content

Reported by: ericlewis Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: Shortcodes Keywords:
Focuses: Cc:

Description

I want to extract data about shortcodes that are in post content.

Attachments (1)

29029.diff (1.9 KB) - added by ericlewis 6 years ago.

Download all attachments as: .zip

Change History (4)

@ericlewis
6 years ago

#1 @ericlewis
6 years ago

  • Keywords has-patch added

In attachment:29029.diff,

introduce get_shortcodes(), which will pass back an array like this:

[ 
  [
    'tag' => 'gallery',
    'attrs' => 
      [
        'ids' => '1,2,3',
        'order' => 'DESC',
        'columns' => '4'
      ]
  ],
  [
    'tag' => 'video',
    'attrs' => 
      [
        'src' => 'http://ericandrewlewis.com/video.mp4',
        'loop' => 'off',
        'autoplay' => 'on'
      ]
  ],
  [
    'tag' => 'gallery',
    'attrs' => 
      [
        'ids' => '4,5,3',
        'order' => 'ASC',
        'columns' => '3'
      ]
  ]
]

get_shortcode_regex() is changed to accept an optional array, which you can pass specific tags to build the shortcode regex with. This means we can do fun stuff like this:

get_shortcodes( $content, 'gallery' )

which would only return you gallery shortcode instances.

or

get_shortcodes( $content, array( 'gallery', 'audio' ) )

which would return you shortcode instances of either gallery or audio.

Last edited 6 years ago by ericlewis (previous) (diff)

#2 @aaroncampbell
6 years ago

Hey Eric, I'm not really sure I see the need here. get_shortcode_regex() gives you 99% of what you need to do this, and while the helper function is nice, I'm not sure it's really needed. I'll think a little more on that.

Having said that, from a technical standpoint, your patch should at least check that the tag names passed to get_shortcode_regex() exist in the keys of $shortcode_tags. Core uses array_intersect() to do this in several other places (list tables and query come to mind, but I'm sure it's used in plenty of other places too).

#3 @aaroncampbell
6 years ago

  • Keywords has-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I'm closing this as won't fix because we don't think the need for get_shortcodes() is high enough to justify it. If we want to look at adding the option to pass a list of shortcode tags into get_shortcode_regex() I think we should open a separate ticket for that.

Note: See TracTickets for help on using tickets.