WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

#6518 closed enhancement (fixed)

Shortcode escaping

Reported by: tellyworth Owned by: westi
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.5
Component: General Keywords: has-patch
Focuses: Cc:

Description

In 2.5.0 there's no official syntax for escaping a shortcode. This makes it difficult to write a post that gives examples of shortcode usage:

Here's how to use the [gallery] tag...

It's possible to work around it by using HTML entities to represent the square brackets but that's ugly.

The enclosed patch improves the shortcode parser to support an escaping syntax:

Here's how to use the [[gallery]] tag...

The output looks like this, with the extra brackets removed:

Here's how to use the [gallery] tag...

It only affects valid shortcode tags, so [[not-a-tag]] displays the input unmodified.

I have unit tested it for common cases including invalid escaping syntax with unbalanced brackets (test_tag_escaped() and test_tag_not_escaped()):

http://svn.automattic.com/wordpress-tests/wp-testcase/test_shortcode.php

Attachments (1)

shortcode-escaping-r7583.patch (1.3 KB) - added by tellyworth 6 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 tellyworth6 years ago

nb, double-escaping works too: [[[gallery]]] outputs [[gallery]].

comment:2 ryan6 years ago

  • Owner changed from anonymous to ryan

comment:3 Denis-de-Bernardy6 years ago

the Markdown escape syntax is also worth looking into:

[gallery] -> not escaped

[gallery] -> escaped

[gallery] -> also escaped

D.

comment:4 Denis-de-Bernardy6 years ago

and wiki formatting needs better escaping as well ;-)

[gallery] -> not escaped

`[gallery]` -> escaped

`` [gallery] `` -> also escaped


comment:5 follow-up: stringfold6 years ago

I agree that this is a problem that needs fixing. (Was a real pain for me until I found a workaround). I would favor the shortcode? format and not those tick marks (unless WP starts using them elsewhere).

I wrote a workaround for my plugin (AZIndex). I simply added another parameter to my shortcode escape="true" for when I wanted to display the shortcode unmodified (in plugin documentation, for example).

But it would be nice to see this fixed in the next release.

comment:6 in reply to: ↑ 5 stringfold6 years ago

Replying to stringfold:

I agree that this is a problem that needs fixing. (Was a real pain for me until I found a workaround). I would favor the shortcode ? format and not those tick marks (unless WP starts using them elsewhere).

I wrote a workaround for my plugin (AZIndex). I simply added another parameter to my shortcode escape="true" for when I wanted to display the shortcode unmodified (in plugin documentation, for example).

But it would be nice to see this fixed in the next release.

Oops -- ? is the escape format I favor.

comment:7 ryan6 years ago

  • Milestone changed from 2.5.2 to 2.9

Milestone 2.5.2 deleted

comment:8 westi5 years ago

  • Milestone changed from 2.9 to 2.8
  • Owner changed from ryan to westi
  • Status changed from new to assigned

comment:9 westi5 years ago

Patch needs a refresh but I think this should go in.

comment:10 westi5 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [10576]) Introduce a simple mechanism for escaping shortcodes by doubling the [[]]. Fixes #6518 props tellyworth.

Note: See TracTickets for help on using tickets.