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.
You must be logged in to reply to this topic.
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βre excited to announce we've released Composer support for installing ACF PRO.
— Advanced Custom Fields (@wp_acf) January 31, 2023
π #ComposerPHP fans rejoice!
β¨ Please see the release post for all the details and full instructions. https://t.co/ebEfp61nlR
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.