How to remove paragraph tags from Advanced Custom Fields outputs

If you’re using the Advanced Custom Fields plugin, you may have found yourself frustrated by WordPress’ forcing of wrapping non-formatted text on pages with paragraph tags when that’s not what you want. Here’s the fix.

When adding a new field, you’ll see a setting called Formatting. Selecting No Formatting from the dropdown turns that field into essentially raw HTML. When you’ve done this but when you add a value into the field when writing your post and you’re still seeing it wrapped in a paragraph tag, it’s because WordPress is built to do just that by default.

Advanced Custom Fields formatting option screenshot

The way around this is to add a function that ignores the wpautop filter set in the core.

Open your theme’s functions.php file and add the following code:


function the_field_without_filters( $the_field=null ) {
    remove_filter('acf_the_content', 'wpautop');
    if( $the_field ) {
      the_field( $the_field );
    } else {
      the_field();
    }
    add_filter('acf_the_content', 'wpautop');
}

Save and you’re good to go.

Post a comment

Fred looking at his camera phone with a dumb grin on his hairy face About

I'm an Ottawa-based web designer and front end developer who's been in the industry since HTML was written in all-caps with inline CSS. I like writing about the web as well as my other creative interests. Follow me on Twitter

Discussion

Requests for help are welcome but my schedule means I may be unable to respond right away.