Displaying Shortcodes within Posts

Posted by Keith on January 19, 2009 at 10:17 am

Digg!

Displaying shortcodes within posts seems like it should be fairly straightforward. However, due to Wordpress’ shortcode processor the shortcodes will be removed when the post is actually displayed to the user. Fortunately this is an easy issue to resolve.

The Issue

Lets say for instance that I wanted to display a small piece of code within a formatted code block (provided by the Syntax Highlighter Plus plugin). This code block contains some PHP code as well as the [REMIX] ... [/REMIX] shortcode used by the WP Remix theme. If I attempt to display the code block as-is the shortcode within the code block will be removed as shown below.

// What should be displayed...
$text = str_replace(array('[REMIX]', '[/REMIX]'),
                    array('', ''),
                    $text);

// What is actually displayed...
$text = str_replace(array('', ''),
                    array('', ''),
                    $text);

The Workaround

The simple workaround for this issue is to encapsulate the shortcode within the same shortcode. An example of this is shown below.

// The format used to display the shortcode properly...
$text = str_replace(array('[[REMIX]REMIX]', '[[/REMIX]/REMIX]'),
                    array('', ''),
                    $text);

// What is actually displayed...
$text = str_replace(array('[REMIX]', '[/REMIX]'),
                    array('', ''),
                    $text);

Fixing the Problem

It seems like the Wordpress developers would have encountered this issue somewhere along the line. Hopefully they are working on an integrated fix for this issue. If you know of any further information on this issue please let me know so that I may update this post accordingly.

You might also be interested in:

Category : Wordpress Tutorials

Leave a comment


Comments

  • No comments yet.

About Us

Double Black Design is committed to providing helpful and insightful information on the topics of software and web development. Read more »

Subscribe

Subsribe via RSS Feed Reader