Fix: Correct Parsing of Final Shortcode Attribute Without Trailing Space #8533
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Trac Ticket: https://core.trac.wordpress.org/ticket/57790
Fix: Correct Parsing of Final Shortcode Attribute Without Trailing Space
Issue Summary
The
shortcode_parse_atts()
function, which usesget_shortcode_atts_regex()
, fails to correctly parse the last attribute of a shortcode when there is no space between the final attribute’s closing quote and the closing bracket]
.Steps to Reproduce
Current Behavior (Bug)
Output:
The last attribute (
money="yes"
) is incorrectly treated as a separate entry instead of being parsed as a key-value pair.Expected Behavior
When a space is included before the closing
]
, the function behaves correctly:Output:
Root Cause
The issue stems from the
get_shortcode_atts_regex()
pattern, where the final attribute pair is not properly matched if there is no space before]
. The previous regex enforced a space (\s
) or end-of-string ($
) after an attribute, causing the last attribute to be misinterpreted.Proposed Fix
The updated regex removes unnecessary lookaheads and ensures correct parsing regardless of whether a space exists before
]
.Old Code:
New Code:
Test Results
After applying the fix, both cases now produce the expected output:
Ensuring that the last attribute is parsed correctly, regardless of spacing.
Impact & Compatibility
shortcode_parse_atts()
is more robust when handling shortcode attributes.