Home › Forums › General Issues › 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.
The topic ‘Filtering content inside shortcodes?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.