Make WordPress Core

Opened 6 years ago

Last modified 5 years ago

#43686 new defect (bug)

Shortcodes containing asterisks may create invalid regex breaking the editor

Reported by: vusys's profile Vusys Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords: has-patch dev-feedback 2nd-opinion
Focuses: administration Cc:


Despite not being a reserved character an asterisk in a shortcode followed by another character will generate invalid regex which breaks the editor.

This code reproduces the issue:

foreach (['*one', '*two'] as $bullet) {
    add_shortcode('*' . $bullet, function () use ($bullet) {
        return 'oh no ' . $bullet;

Attached is a gif showing the editor tabs not working correctly along with the error in the console in Chrome 65 and Firefox 59.

I know it's unconventional to use an asterisk in a shortcode, but my use case is porting a legacy CMS that used BBCode into WordPress while trying to keep as much of the original formatting possible for old content.

This is an issue with all plugins deactivated (except for the above code) using the Twenty Seventeen theme.

Attachments (3)

2018-04-03 22.30.25.gif (725.5 KB) - added by Vusys 6 years ago.
Screen Shot 2018-04-03 at 22.33.15.png (102.4 KB) - added by Vusys 6 years ago.
console errors
43686.diff (709 bytes) - added by soulseekah 6 years ago.
wordCount patch

Download all attachments as: .zip

Change History (8)

6 years ago


6 years ago

console errors

6 years ago

wordCount patch

#1 @soulseekah
6 years ago

  • Keywords has-patch dev-feedback 2nd-opinion added

Welcome to Trac! :)

The Word count functionality breaks if there's a registered shortcode with asterisk character in it.


add_shortcode( '*stars*', function() {
	return '★★★';
} );

Note, that the * symbol is specifically allowed in shortcode names as per the tests:

Tests_Shortcode::test_registration_good with data set #2 ('unreserved!#$%()*+,-.;?@^_{|}~chars', true)

The error?

word-count.js?ver=5.0-alpha-42125-src:64 Uncaught SyntaxError: Invalid regular expression: /\[\/?(?:wp_caption|caption|gallery|playlist|audio|video|embed|*stars*)[^\]]*?\]/: Nothing to repeat
    at new RegExp (<anonymous>)
    at new WordCounter (word-count.js?ver=5.0-alpha-42125-src:64)
    at post.js?ver=5.0-alpha-42125-src:1270

this.settings.shortcodesRegExp·=·new·RegExp(·'\\[\\/?(?:'·+·shortcodes.join(·'|'·)·+·')[^\\]]*?\\]',·'g'·);, where shortcodes is a JSON object:

/* <![CDATA[ */
var wordCountL10n = {"type":"words","shortcodes":["wp_caption","caption","gallery","playlist","audio","video","embed","*stars*"]};
/* ]]> */

43686.diff patches the regular expression escaping all special characters as needed.

I suspect that other issues may arise, since there may be other regular expressions in there. Your QTags error is probably related, but it would be crucial to see your QTags addition code to see what you're doing, otherwise no other errors are occurring on a clean WordPress install.

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

#2 @Vusys
6 years ago

I'm not adding any Quicktags. It looks like that error only occurs when creating a fresh post (post-new.php), but not when editing an existing post (post.php).

#3 @SergeyBiryukov
6 years ago

  • Milestone changed from Awaiting Review to 5.0

#4 @pento
6 years ago

  • Milestone changed from 5.0 to 5.1

#5 @pento
5 years ago

  • Milestone changed from 5.1 to Future Release
  • Version 4.9.4 deleted

This issue needs further investigation.

Note: See TracTickets for help on using tickets.