0

I use Classic Editor and try to remove tag <p> from <blockquote> Now I have

<blockquote><p><q>Smart text.</q></p></blockquote>

and I need

<blockquote><q>Smart text.</q></blockquote>

I need it only for this tag. I tryed use this variant also I used this filter.

function remove_paragraphs_inside_blockquotes( $content ) {
    $content = preg_replace( '/<blockquote>\s*<p>(.*?)<\/p>\s*<\/blockquote>/is', '<blockquote>$1</blockquote>', $content );
    return $content;
}
add_filter( 'the_content', 'remove_paragraphs_inside_blockquotes', 9 );

But it didn't help for me. Can someone help ?

1 Answer 1

0

try this

add_filter( 'the_content', 'remove_paragraphs_inside_blockquotes', 9  );

function remove_paragraphs_inside_blockquotes( $content ) {
    $dom = new DOMDocument();

    libxml_use_internal_errors(true);
    $dom->loadHTML('<?xml encoding="utf-8" ?>' . $content, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
    libxml_clear_errors();

    $blockquotes = $dom->getElementsByTagName('blockquote');
    
    foreach ( $blockquotes as $blockquote ) {

        foreach ( $blockquote->childNodes as $child ) {
            if ( $child->nodeName === 'p' ) {
                while ( $child->firstChild ) {
                    $blockquote->insertBefore($child->firstChild, $child);
                }
                $blockquote->removeChild($child);
            }
        }
    }

    $content = $dom->saveHTML();

    return $content;
}

for display

<?php echo apply_filters( 'the_content', wp_kses_post( get_the_content() ) ); ?>
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 23 at 18:13

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.