WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 20 months ago

Last modified 20 months ago

#12982 closed feature request (wontfix)

Shortcodes don't allow shortcodes in attributes

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

Description

I think that shortcodes should work when used inside attributes of other shortcodes, just like they can work inside the "$content" of a shortcode.

Some people says that that isn't supposed to work, but theoretically it is supposed to work when using the "do_shortcode" function. It don't work because:

1) "do_shortcode" is context free, which it's actually fine by its fastness, although it don't allow stuff like inserting a shortcode inside the content of itself

2) The shortcode regex stops with the first occurrence of a ] in a shortcode, so in this:

[foo bar="[baz /]"]content[/foo]

it "outputs":

[foo bar="[baz /]

and "]content[/foo] is ignored.

I'm not programmer but I tried to fix it by changing the shortcode regex so it allow anything between quotes or brackets inside the first group of brackets. It sort of works, but also mess the handling of quotes in the rest of the shortcode, so I think something more advanced have to be done to make it work.

This is somewhat related to another shortcode tickets, but none of them addresses nor solves this specifically.

Attachments (1)

diff-sc.diff (763 bytes) - added by Atoon 5 years ago.

Download all attachments as: .zip

Change History (10)

@Atoon5 years ago

comment:1 @Atoon5 years ago

I attached a diff that solves that particular problem. It fixes the 3rd capturing group to ignore anything between brackets and low the priority of do_shorcode to 9, to avoid conflicts with wptexturize. It may not have problems with wpautop since that's is somewhat fixed with shortcode_unautop.

comment:2 @nacin5 years ago

  • Keywords needs-unit-tests added; shortcode attributes removed
  • Milestone changed from 3.1 to Future Release
  • Type changed from defect (bug) to feature request

comment:3 follow-up: @mikeschinkel5 years ago

  • Cc mikeschinkel@… added

While I am normally not the one to disagree with a feature request this does strike me as violating a lot of the assumptions of attributes in HTML/XHTML after which shortcodes are at least partially modeled. I fear potential unintended consequences (read: "hard to squash bugs") if this were enabled.

Can you give some example use cases where this would really be important? You've described in abstract but I don't see examples that demonstrate a need that can only be accomplished using your proposal.

comment:4 in reply to: ↑ 3 ; follow-up: @Atoon5 years ago

Replying to mikeschinkel:

While I am normally not the one to disagree with a feature request this does strike me as violating a lot of the assumptions of attributes in HTML/XHTML after which shortcodes are at least partially modeled. I fear potential unintended consequences (read: "hard to squash bugs") if this were enabled.

Can you give some example use cases where this would really be important? You've described in abstract but I don't see examples that demonstrate a need that can only be accomplished using your proposal.

Indeed it feels unnatural, but in general a plain regex don't behave exactly like normal HTML tags, like in the impossibility of embed a same shortcode inside the content of itself, as far as I know. In an attribute is possible, thought.

I was trying to replicate the usage of MediaWiki templates (or "transclusion"), which allow to create snippets with custom snippets and markup inside the attributes and even with parse functions (which can already be done with PHP). An example, although a bit extreme. I was trying to update an old plugin with a similar function, but since shortcodes is already the most similar approach I thought it could fit.

comment:5 in reply to: ↑ 4 @mikeschinkel5 years ago

  • Cc mikeschinkel@… removed

Replying to Atoon:

Indeed it feels unnatural, but in general a plain regex don't behave exactly like normal HTML tags, like in the impossibility of embed a same shortcode inside the content of itself, as far as I know. In an attribute is possible, thought.

If robustness is a concern I had the chose between a regex and a parser I'd not choose the regex solution...

Replying to Atoon:

I was trying to replicate the usage of MediaWiki templates (or "transclusion"), which allow to create snippets with custom snippets and markup inside the attributes and even with parse functions (which can already be done with PHP). An example, although a bit extreme. I was trying to update an old plugin with a similar function, but since shortcodes is already the most similar approach I thought it could fit.

Okay, so I see your inspiration for the request but I still don't see a specific use-case that you are trying to accomplish. Can you give some concrete examples?

Also, rather than:

[foo bar="[baz /]"]content[/foo]

Why would this not be a workable solution?

[foo][bar][baz /][/bar]content[/foo]

comment:6 follow-up: @Atoon5 years ago

I thought something like an infobox:

[infobox-cd
name="Abbey Road"
cover="/laserdisc.jpg"
artist="The Bombay Beach Boys"
country="[flag id=mx] Argentina<br />[flag id=dk] Vanuatu (post-production)"
rating="[stars no="3"]"
prev="[[The Wall]]"
next="[[Born in the U.S.A.]]" /]

It allow a shortcode to display an icon and allow brackets inside quotes to have wiki-like internal links, with a plugin. Also, having it like a shortcode helps to change the layout later while retaining the data.

In general I want to have more wiki-like functionality in WordPress. MediaWiki has everything I need but feels overwhelming for private use, adds to many stuff and pages I don't need and you can't have pretty permalinks (yeah, that's an issue). Of course, not every wiki-like function should be on core as several stuff exist as plugins or even in core like revisions, but at least some current core stuff can be tweaked a bit to don't resort to much hacking.

It's probably that I can resort to custom fields and WP template tweaks for the example given, and its true for most elaborate CMS implementations, but by the sandbox nature of a wiki-page some pages may require more snippets than a single fixed one.

comment:7 in reply to: ↑ 6 @mikeschinkel5 years ago

Replying to Atoon:

It allow a shortcode to display an icon and allow brackets inside quotes to have wiki-like internal links, with a plugin. Also, having it like a shortcode helps to change the layout later while retaining the data.

Ahhh. So it's your desire to mix wiki-like functionality with shortcode functionality using the same syntax as MediaWiki. IMO, not a good idea.

I would argue the following would be much better in the context of WordPress and it's something you can do today w/o enhancements to core simply by writing your own shortcode:

[infobox-cd
    name="Abbey Road"
    cover="/laserdisc.jpg"
    artist="The Bombay Beach Boys"
    prev="{{The Wall}}"
    next="{{Born in the U.S.A.}}"]
  [country id="mx"][flag] Argentina[/country]
  [country id="dk"][flag] Vanuatu (post-production)[/country]
  [rating stars="3"]
[/infobox]

In general I want to have more wiki-like functionality in WordPress. MediaWiki has everything I need but feels overwhelming for private use, adds to many stuff and pages I don't need

Sounds good, but I think you are better off using a different internal link syntax than MediaWiki, like the double braces I suggested by implication in my example.

Each syntax has it's appropriate context and within WordPress wikipedia's exact syntax just isn't appropriate. Personally I wanted to see URI Template syntax used for permalink structures because it's likely to become a standard but finally realized that in WordPress URL templates using the "%var%" syntax and I became "one" with it.

and you can't have pretty permalinks (yeah, that's an issue).

Yeah you can, it's just a lot of work (but getting easier.) Hook "template_redirect" and you can do whatever you want.

You might also be interested in the ticket I'd like to most see included in core: #12935.

Of course, not every wiki-like function should be on core as several stuff exist as plugins or even in core like revisions, but at least some current core stuff can be tweaked a bit to don't resort to much hacking.

You can get the above without any hacking of core. And frankly, it seems like a pretty elegant syntax, IMO.

comment:8 @iseulde20 months ago

  • Keywords needs-unit-tests removed
  • Resolution set to wontfix
  • Status changed from new to closed

comment:9 @helen20 months ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.