Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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
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!