Title: Shortcodes do not work when preg_replace content
Last modified: August 22, 2016

---

# Shortcodes do not work when preg_replace content

 *  Resolved [MariuszEm](https://wordpress.org/support/users/mariuszem/)
 * (@mariuszem)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/)
 * Hi,
    I am using some preg_replace I did write some time ago:
 *     ```
       $text = preg_replace('/(\s)(\w)(\s)/', ' $2&nbsp;', get_the_content());
       echo $text;
       ```
   
 * It changes every one letter words like ” a ” to ” a “, so non-breaking space 
   force it not to stay on the end of line.
 * But when I use preg_replace, shortcodes do not work, so [my_sc] appers as [my_sc],
   and not custom, shortocode code.
 * How can I preg_replace all content without using my code in single.php, without
   preventing shortcodes work?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5247977)
 * Kindly enclose your examples in backticks to preserve the actual code. Without
   the backticks, there is no difference in your two instances of `[my_sc]`, and
   we cannot tell what the preg_replace is doing to it.
 *  Thread Starter [MariuszEm](https://wordpress.org/support/users/mariuszem/)
 * (@mariuszem)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5247988)
 * Preg replace, replaces ” a ” to ” a nbsp;”
    My shortcode is echoing “test”, but
   it do not work. WP prints out shortcode tag, as it is, not “test” word. It is
   coused by preg-replace, becouse when I change code to simple `the_content();`
   everything works correctly. In place of shortcode i got “test”.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5247995)
 * The problem is that you are bypassing the do_shortcode() function. Try changing
   your code to this:
 * `$text = preg_replace(‘/(\s)(\w)(\s)/’, ‘ $2 ‘, get_the_content());
    echo do_shortcode(
   $text );
 *  Thread Starter [MariuszEm](https://wordpress.org/support/users/mariuszem/)
 * (@mariuszem)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5248038)
 * Man, you’re life saver;)
    Works like a charm now.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5248126)
 * If you want all filters applied to your content, not just do_shortcode(), and
   you want your preg_replace() to apply to the filtered result, this should work:
 *     ```
       $text = apply_filters( 'the_content', get_the_content() );
       $text = preg_replace( '/(\s)(\w)(\s)/', ' $2 ', $text );
       echo $text;
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Shortcodes do not work when preg_replace content’ is closed to new replies.

## Tags

 * [preg_replace](https://wordpress.org/support/topic-tag/preg_replace/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [the_content](https://wordpress.org/support/topic-tag/the_content/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/shortcodes-do-not-work-when-preg_replace-content/#post-5248126)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
