Support

Account

Home Forums General Issues Filtering content inside shortcodes?

Solving

Filtering content inside shortcodes?

  • Hi, I’m using the below to filter <br> and <p> tags out of shortcodes using $content. This works great within the WP editor, but does not work within ACF.

    Can anyone help point me in the right direction? Thanks!

    function the_shortcode_filter($content) {
        $block = join("|",array("columns", "first", "second", "block-bullets", "loan-terms-from"));
        $rep = preg_replace("/()?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
        $rep = preg_replace("/()?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
    return $rep;
    }
    add_filter("the_content", "the_shortcode_filter");
  • ACF does not run ‘the_content’ on wysiwyg fields. Add a second filter

    
    function the_shortcode_filter($content) {
        $block = join("|",array("columns", "first", "second", "block-bullets", "loan-terms-from"));
        $rep = preg_replace("/()?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
        $rep = preg_replace("/()?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
    return $rep;
    }
    add_filter("the_content", "the_shortcode_filter");
    add_filter("acf_the_content", "the_shortcode_filter");
    
  • Hi John, thanks for the quick reply. I tried adding the second filter but it still didn’t seem to solve the issue.

    Could my method of creating the shortcode be affecting anything?

    function twoCol( $atts, $content = null ) {
        return
        '<div class="row shortcode">' . do_shortcode($content) . '</div>';
    }
    add_shortcode("columns", "twoCol");
  • It could have something to do with the order that the filters are run. ACF does run shortcodes on wysiwyg fields.

    Since it appears that your filter is altering the content of the shortcode before it is run, it could be that the shorcode is run before your filter. You could try setting a high priority on the filter for ACF to make the changes before ACF runs the rest of the content filters.

    
    add_filter("acf_the_content", "the_shortcode_filter", 1);
    
  • Still no luck. Thanks again for looking into this.

    If I come across a solution, or find what I might be doing wrong, I’ll definitely post the fix here.

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

You must be logged in to reply to this topic.