WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 7 months ago

#52517 new defect (bug)

Shortcodes inside AMP tags do not work

Reported by: Krstarica Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Formatting Keywords:
Focuses: Cc:

Description

Example code to demonstrate shortcode not working inside AMP tag:

add_shortcode ( 'test', '__return_false' );
echo do_shortcode('<amp-ad width=320 height=100 [test]>');

displays

<amp-ad width=320 height=100 [test]>

Example code to demonstrate shortcode working inside regular tag:

add_shortcode ( 'test', '__return_false' );
echo do_shortcode('<img width=320 height=100 [test]>');exit;

displays

<img width=320 height=100 >

The reason is wp_kses_attr_parse function not supporting tags with hyphens. All AMP tags contain hyphen, e.g. "amp-ad":
https://amp.dev/documentation/guides-and-tutorials/learn/spec/amphtml/?format=websites#html-tags

var_dump(wp_kses_attr_parse( '<amp-ad width=320 height=100>' ))

displays

bool(false)

instead of

array(4) {
  [0]=>
  string(7) "<amp-ad "
  [1]=>
  string(10) "width=320 "
  [2]=>
  string(10) "height=100"
  [3]=>
  string(1) ">"
}

The fix is to change function wp_kses_attr_parse( $element ) in wp-includes/kses.php from:

$valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches );

to

$valid = preg_match( '%^(<\s*)(/\s*)?([a-zA-Z0-9\-]+\s*)([^>]*)(>?)$%', $element, $matches );

Change History (1)

#1 @westonruter
7 months ago

  • Component changed from Shortcodes to Formatting
  • Severity changed from critical to normal
  • Version changed from 5.6.1 to 4.7

Seems to be something that was missed in #34105.

Note: See TracTickets for help on using tickets.