Gravity Forms shortcode getting extra line breaks when used with ACF

ACF apparently expands shortcodes before returning content, which means if you attempt to run it through the_content filter, any line breaks will get hit by wpautop. We fought and fought and fought, moving filter orders around, removing filters, etc., and finally decided to not fight WordPress, but instead change Gravity Forms.

Below is a simple filter that collapses all whitespace including line breaks, tabs and regular spaces, and just replaces it with a single space. This should preserve whatever Gravity Forms is doing, and also address WordPress’s later escaping.

add_filter(
    "gform_shortcode_form",
    static function ($shortcode_string, $attributes, $content) {
        return trim(preg_replace('/\s+/', ' ', $shortcode_string));
    },
    10,
    3
);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.