• Resolved imtino

    (@imtino)


    How do you remove p tags from images?

    With the Classic Text Editor, I was able to successful achieved that by this code.

    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('acf_the_content', 'filter_ptags_on_images');
    add_filter('the_content', 'filter_ptags_on_images');

    But that doesn’t work with Gutenberg.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Marius L. J.

    (@clorith)

    Hi,

    This shouldn’t be an issue with Gutenberg, as it does not add <p> tags around images, but instead uses the appropriate <figure> wrapper.

    If there’s any special circumstances for you having them there, the filter you are using is still valid, everything is still passed through our the_content filter before being displayed.

    Thread Starter imtino

    (@imtino)

    Sorry, you’re right. When I first commented, I didn’t understand blocks. The image block is fine. It doesn’t add the <p> tags.

    It is the classic block that add the <p> tags. After installed Gutenberg, it automatically turned all my old posts into classic block. Which is fine. I kept most of them classic. It’s too much work right now to change at once.

    So the issue is in classic block, the filter doesn’t work.

    I have to convert everything to using Gutenberg’s blocks especially the image block because I don’t want those <p> tags.

    Moderator Marius L. J.

    (@clorith)

    Your existing filter should work for that, since the classic block is still just a part of the content. I’d mention that we did some changes to how it treats paragraphs (to hopefully remove some extra added ones) in Gutenberg 3.4.0, it won’t have an effect on content that’s already been saved inside a classic block though, but any new ones will hopefully function much better for you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Remove P tags from images’ is closed to new replies.