Ticket #6518 (closed enhancement: fixed)

Opened 4 years ago

Last modified 3 years ago

Shortcode escaping

Reported by: tellyworth Owned by: westi
Priority: normal Milestone: 2.8
Component: General Version: 2.5
Severity: normal Keywords: has-patch
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

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

Change History

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

comment:2   ryan4 years ago

  • Owner changed from anonymous to ryan

the Markdown escape syntax is also worth looking into:

[gallery] -> not escaped

[gallery] -> escaped

[gallery] -> also escaped

D.

and wiki formatting needs better escaping as well ;-)

[gallery] -> not escaped

`[gallery]` -> escaped

`` [gallery] `` -> also escaped

comment:5 follow-up: ↓ 6   stringfold4 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   stringfold4 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   ryan4 years ago

  • Milestone changed from 2.5.2 to 2.9

Milestone 2.5.2 deleted

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

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

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

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

Note: See TracTickets for help on using tickets.