3

I made a plugin that adds a shortcode with optional content. If there's no content, WordPress still tries looking for a closing tag. This is clearer with an example:

[span class="foo"]
[span class="bar"]
[span class="baz"]stuff[/span]

Wanted:

<span class="foo"></span>
<span class="bar"></span>
<span class="baz">stuff</span>

Actual:

<span class="foo">
  [span class="bar"]
  [span class="baz"]stuff
</span>

Is there a way to make WordPress produce the first output? I'm expecting many of the plugin's users to be confused by this behavior. One way is to modify the_content before do_shortcode runs, but it's pretty hacky. Is there a clean or existing way to change this behavior?

Edit: I'm not asking why this behavior occurs, I'm asking for a good way to change this behavior.

1
  • It's the same thing because that's exactly what WordPress does: add_filter( 'the_content', 'do_shortcode', 11 );. What I meant was I can add a new filter to pre-process my shortcode. E.g. I can make shortcodes self-close before do_shortcode runs. However, I think this is a hacky solution.
    – Leo Jiang
    Jul 31, 2016 at 20:08

2 Answers 2

10

Wordpress interpreted your shortcode like this:

Wordpress thought your shortcode like

The main issue being, that you have a unclosed shortcode of the same tag in front of an enclosing shortcode of the same tag, which won't be parsed correctly. The documentation states that you might run into problems with unclosed shortcodes.

When you call your shortcode like this:

[span class="foo" /]
[span class="bar" /]
[span class="baz"]stuff[/span]

You will get your expected result.

Because the self-closing marker / is needed in your use-case, although it generally is considered optional, but as it forces the parser to ignore following closing tags it gets you your expected result.

The above solution is the correct usage of shortcodes according to the WordPress Shortcode API. If you want to to pre-process your shortcode in one way or another you can do that, but generally just make your users use the correct syntax in the first place.

7
  • 5
    +1 the problem is improper use of shortcodes, not the way wordpress handles them Jul 15, 2016 at 8:29
  • +1 for the excellent graphic (a picture is worth 1KB of words)
    – scott
    Jul 15, 2016 at 18:10
  • I'm writing a plugin and I'm expecting this to cause a lot of support requests. This is why I want to change this behavior rather than forcing the plugin's users to add a slash. Most shortcode plugins use either self-closing or enclosing shortcodes, not both. Since mine uses both, I'm expecting it to cause confusion.
    – Leo Jiang
    Jul 15, 2016 at 18:59
  • @MarkKaplun this is proper use of shortcodes: codex.wordpress.org/Shortcode
    – Leo Jiang
    Jul 15, 2016 at 19:01
  • 3
    @LeoJiang,I always wonder why people ask experts and then argue with them. you are free to do whatever you want, but non closed short code is like non closed html, the browser might make it work, but it might not. Jul 16, 2016 at 3:51
0

A shortcode has nothing to do with opening and closing tags! A shortcode is a way to execute a function and output the results into content.

add_shortcode('hello', myFunction); function myFunction (){return 'Hello world';}

Putting the shortcode [hello] in your post will output Hello World.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.