Have Camera Will Travel - Reviews & Tips HomepageHave Camera Will Travel - Reviews & Tips

David Coleman Photography

  • Reviews & Tips
  • GoPro
  • Lightroom
  • Places
  • Photos
  • Reviews
    • Nikon
    • Sony
    • Fujifilm
    • Cameras
    • Lenses
    • Sample Images
    • Travel Tripods
    • Adventure Cameras
    • 360°
  • Memory Cards
  • GoPro
  • Flash
  • Travel Tripods
  • Books
  • Social Media
    • Facebook
    • Instagram
    • YouTube
    • Twitter

How to Escape Shortcodes in When Posting Code Snippets in WordPress

If you're trying to post example shortcodes in your post but finding that the shortcode is getting processed, here's how to escape it and present the shortcode as plain text.

wp shortcode text dummy code 1068x303 - How to Escape Shortcodes in When Posting Code Snippets in WordPress

Categories: Wordpress for Photographers
Tags: Wordpress tricks
Last updated about 4 months ago // Originally published about 4 years ago

This is another one of those very simple solutions to a frustrating problem, but a solution that’s surprisingly difficult to find an answer to on the web. I’m posting it here not because it’s especially innovative, but simply because I had trouble finding the solution elsewhere and hopefully this will save someone else some time. And for my own future reference too, for that matter.

I was recently writing up a post about changing the “Filed Under” and “Tagged With” metadata labels for WordPress posts, and I needed to include shortcode snippets in the examples. The problem is, those shortcodes were being rendered even in text designated a code block. So I needed a way to exclude specific shortcodes in the text from being processed. Using a backslash, as you might in some coding languages and regex, doesn’t work.

This only applies to valid shortcodes that are being called on your WordPress installation. If it’s not a valid, installed shortcode like the nonsense one I’m using on this page as the example, processing it doesn’t do anything so it’s just treated as plain text.

There are plugins that will do it, along with other features. The Raw HTML is one I’ve used for other reasons in the past that has worked well. But I figured there had to be a way to escape the shortcode without installing yet another plugin, and especially a plugin that might have unintended consequences elsewhere on the site.

Convert Square Brackets to Unicode

In theory, converting the square brackets to unicode should work. You’d replace:

[ with [
] with ]

to create something like:

[shortcode_here]

But for whatever reason, that didn’t work for me. Nor did processing it with an HTML encoder. It might have had something to do with using it in a code block. So I’ve used the next option.

Double the Brackets

In the end, I found the solution in a ticket from 2007 in the WordPress core issue tracker. It was something baked into WordPress Core way back in version 2.5. And it’s very simple. Just double the brackets.

A shortcode with single brackets will be processed. By doubling the brackets, you’re escaping the square brackets.

So instead of typing:

[shortcode_here]

You’d type:

[[shortcode_here]]

With Opening and Closing Shortcodes

If you’re doing a line with opening and closing shortcodes, you’d only double-bracket the first and last instances, like this:

[[shortcode_here]TEXT HERE.[/shortcode_here]]

Like I said, it’s really simple. But probably because not many people need it very often, it’s not widely publicized. But if you’re one of those handful of people who do need it, hopefully this saves you some time and frustration.

By David Coleman
Instagram: @havecamerawilltraveldc

Last updated on September 4, 2020

Categories: Wordpress for Photographers
Tags: Wordpress tricks

I may earn a commission for purchases using links on this page. Learn more.

News & Deals

With the holidays fast approaching, there’s some great deals on photography gear and services available. I’ve put together a roundup of Black Friday and Cyber Monday 2020 Photography Deals. It’s a running list as new deals are announced, so be sure to check back.

Canon announces new EOS Rebel T8i DSLR. Canon has announced their new T8i DSLR. 24MP APS-C cropped CMOS sensor, 4K24 video, and updated image processing and detection systems. It’s available for preorder from B&H Photo.

New Firmware for the Ricoh GR III. Ricoh has release firmware v1.31 for the GR III. Release notes are: “Corrected — When shooting with the multiple exposure, in rare cases the live view may not display correctly. Improved stability for general performance.” You can download it here.

New Canon 1D X Mark III Pro DSLR Announced. Canon has announced their new flagship model of pro DSLR, the 1D X Mark III. They’re sticking with a 20.1MP CMOS sensor, but it’s a new and improved version and boasts an incredible native ISO range of ISO 100 to ISO 102400 and an extended range that goes up to a jaw-dropping ISO 819200. With revamped autofocus, a new image processor, and all sorts of other bells and whistles, it’s no wonder that Canon is billing it as “The Ultimate EOS.”

New Manfrotto Pro Rugged Memory Cards. Manfrotto, an Italian probably best known for their tripods and studio accessories, has launched a new Pro Rugged range of high-end SD, microSD, and CompactFlash cards. While they don’t have a huge selection of sizes and speeds, the cards are aiming for very fast cards that can put up with rugged demands. For instance, they claim that their V90 rated SD cards can withstand three times the force (20kg / 45 lbs) of a regular SD card. I’ve ordered some to run through my speed tests.

New Sony Alpha a7 firmware released. A firmware update for the Sony a7R IV now lets you control external Sony flashes from the camera’s own menu system. Up to 15 flashes in five groups can be controlled via the camera. It also applies to the a7R III and a7 III.

Kinsta Wordpress Managed Hosting

Comments

  1. uwb says

    April 2, 2020 at 5:54 am

    That’s great!!!
    and:
    I’ve seen, that shortcodes with start and end have to be treated like this:

    [[hello]Text between shortcode[/hello]]

    chears
    uwb

    Reply
  2. Amrit Ray says

    December 4, 2019 at 1:54 am

    This is no longer working. It is processing the shortcode inside the [] brackets :(

    Reply
    • David says

      December 4, 2019 at 9:40 am

      I just tried it on my site, and it’s working for me. I guess some difference in the way our sites are set up makes it process it differently.

      Reply
    • WIlliam Rey says

      September 4, 2020 at 8:09 am

      It works. But you have to carefully double the first open bracket of the opening part and double the last close bracket of the closing part. As shown in the comments of April 2th 2020.

      It does NOT work if you double the brackets everywhere.

      Reply
  3. Rodolfo says

    February 15, 2019 at 7:24 am

    Thank you so much! Didn’t know that so that’s awesome :)

    Reply

Questions & Comments Cancel reply

You have to agree to the comment policy.

About Me

I take photos. I travel. I do it for a living. Seven continents. Dozens of countries. Up mountains. Under water. And a bunch of places in between.

Get in Touch

Newsletter

© 2009–2021 David Coleman · Have Camera Will Travel LLC · all rights reserved


Photos by David Coleman. Please contact me for licensing images.
All images are registered with the U.S. Copyright Office.


Privacy & Disclosure · Cookie Declaration · Disclaimer · Terms of Service · Copyright · Contact


XML Sitemap