• Resolved MariuszEm

    (@mariuszem)


    Hi,
    I am using some preg_replace I did write some time ago:

    $text = preg_replace('/(\s)(\w)(\s)/', ' $2 ', 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)
  • 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

    (@mariuszem)

    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”.

    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

    (@mariuszem)

    Man, you’re life saver;)
    Works like a charm now.

    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.