Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#31065 closed defect (bug) (wontfix)

Shortcode without closing tag

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



I've just found a bug with shortcodes when developing my theme. The problem is if we put 2 instances of a shortcode next to each other, one without closing tag, one with closing tag, then they don't work as expected.

This is sample code:

add_shortcode( 'test', 'rw_test' );

function rw_test( $atts, $content )
	$atts = shortcode_atts( array(
		'name' => 'WordPress',
	), $atts );

	return $atts['name'];

echo do_shortcode( '[test][test name="Anh"][/test]' );

The expected result is "WordPressAnh", but it displays only "WordPress".

I think the regular expression has a bug here, but it looks too complicated to me so I hope someone can join and fix this bug.


Change History (6)

This ticket was mentioned in Slack in #core by rilwis. View the logs.

7 years ago

#2 @pento
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version trunk deleted

Thanks for the bug report!

This is expected behaviour from the shortcode parser. In your particular example, there's no way to tell which [test] shortcode the [/test] closing tag belongs to, so the parser assumes it belongs to the first one it encounters.

If you change your example to this:

echo do_shortcode( '[test][/test][test name="Anh"][/test]' );

It will function as expected.

#3 @rilwis
7 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

I don't think that's expected result.

What I posted is just an example code. What happens if we put [test] somewhere in the first paragraph of post content, and the second one in very bottom of that (which means there're a lot of content between them)? The result can't be predicted. And the most important thing is they're valid shortcodes and should work properly.

If we can't solve this problem, then we have to force user to always close shortcodes, because any shortcode that works without closing tag can cause the similar problem.

#4 @SergeyBiryukov
7 years ago

  • Milestone set to Awaiting Review

#5 @pento
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed

In this scenario, it's still not possible for us to assume that the behaviour you're describing will always be expected - it's a legitimate use case to want to nest shortcodes, even across multiple paragraphs.

Given that the existing behaviour has existed for a very long time, there's no way to change it without breaking a lot of sites.

#6 @rilwis
7 years ago

The existing behaviour is fine and I don't mean to change it. What I mean is it doesn't consider every cases and I hope there's a work around that to improve that.

The most important thing is we are told that the shortcode without closing tag is valid if it doesn't need any content. Everyone is told that. But if we have 2 such shortcodes in the post content, then it breaks the post.

That actually is a bug and should be fixed.

Note: See TracTickets for help on using tickets.