[wp-trac] [WordPress Trac] #50683: Parse content for shortcodes instead of using regex

WordPress Trac noreply at wordpress.org
Thu Jul 16 16:02:50 UTC 2020


#50683: Parse content for shortcodes instead of using regex
-------------------------+-----------------------------
 Reporter:  cfinke       |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Shortcodes   |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Shortcodes are currently "parsed" out of content using a regular
 expression and a call to `preg_replace_callback()`. This causes some
 issues like the inability to use a square bracket in a shortcode
 attribute.

 I've written and attached to this ticket a lexer/parser that steps through
 the content character by character, finding shortcodes and calling
 `do_shortcode_tag()` for each one as soon as it's completely parsed.  It
 passes every existing shortcode unit test, as well as six additional tests
 I've added. It fixes tickets #49955 and #43725 and may possibly fix others
 that deal with shortcode edge cases.

 This method has the advantage of being able to deal with content that
 wouldn't be properly extracted by a regular expression. Take this string
 for example:

 {{{
 [shortcode1][shortcode2 att="[/shortcode1]" /][/shortcode1]
 }}}

 The current implementation would parse this as a single shortcode:

 * `[shortcode1]` with inner content `[shortcode2 att="`

 The parse I've attached properly recognizes it as two shortcodes:

 * `[shortcode1]` with inner content `[shortcode2 att="[/shortcode1]" /]`
 * `[shortcode2]` with attribute `att="[/shortcode1]"`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50683>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list