Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

strip p tags around img and iframe tags in wordpress wysiwyg field

i try to get the p tags stripped around img and iframe tags in the wordpress wysiwyg field - unfortunately it doesn't work. i've tried the following function but no luck:

function filter_ptags_on_images($content)
{
    $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
    return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

is there an error inside or does anyone have a more solid solution? best regards ralf

ahhhh found the error. the code actually works i had to replace the_content with acf_the_content and all is fine.

1 Answer

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Nice fix Ralf! Just noting here that you got the problem resolved switching the_content to acf_the_content. Thanks!