Solution: Extra P and BR tags included while using ACF (Advanced Custom Fields)

June 13th, 2017 Wordpress 606 views

Hey Guys, this is something I have been looking for from couple of days. Finally got the solution on my own.

Problem

While using ACF (Advnaced Custom Fields) plugin on a WordPress site, when you add WYSISYG editor field, it stores the custom field data properly without any issues. But when you print that data on frontend, it ends up printing it with extra P and BR tags. This happens mainly when you are using some shortcodes inside this WYSISYG field.

So I tried contacting directly with ACF Support guys and they suggested me to use the below solution.

Please note that section_content is the field name I am using. I tried this solution but what it did is, it simply converted the content into Plain text. But what’s the different between simple Textarea and WYSIWYG field then?

So I decide to post the issue over our favourite place stackoverflow.com 🙂
I got some answers over there and most of them were suggesting me to use “the_content” filter as shown in below code.

Now this solved the issue partially as it removed some of the extra BR and P tags but still it was not removing the tags that were added before and after the shortcodes I was using in WYSIWYG field.

As internally, this filter get applied already while getting the field value. So it got applied twice if we use the above mentioned code.

Solution

It took a while to understand the issue but the solution was in front of me. I simply combined the solution of ACF support guys and Stackoverflow guys using below code.

And that fixed it. So what it did was, it converted the whole content into plain text first. And then it applied the the_content filter, so the shortcodes got compiled properly.

If you are facing similar issue, then make sure to use the above 2 lines before printing/echoing any WYSISYG field. Make sure you change the section_content with your provided field name.

I hope the ACF guys fix this issue in future updates.

If you found this helpful, please share it with others and don’t forget to leave your comments.

Leave your comments: