I'm fine with P tags for most of the content, since it makes styling easier. However I'd like to have span div iframe and possibly other HTML tags to not be placed inside of a p for post.

I saw this question but it doesn't quite solve it. Also, I saw the linked CSSTricks article, and tried a sample code that used iFrame on it for my spans. Am I doing something wrong with the code? Or what should I do to remove the p tags from html tags in my post.

Here's my code that isn't working:

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*(<span .*>*.<\/span>)\s*<\/p>/iU', '\1', $content);
} add_filter('the_content', 'filter_ptags_on_images');
  • Am I correct in saying that you would like to apply the the_content filter to remove all span, div and iframe tags that reside inside a p-element?
    – engelen
    Aug 23, 2015 at 10:41

2 Answers 2


To me you code looks good, I would recommend adding priority as high as 9999 Heres how :

add_filter('the_content', 'filter_ptags_on_images', '9999');

Try this. Open your functions.php file and add the following PHP snippet

remove_filter('the_content', 'wpautop');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.