strip only specific tags (like ), but keep other tags (like )

I know it’s easy to disable WordPress from adding both p and br tags with:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

but I want WordPress to keep adding <br> where there is a line break.
I only use text editor, visual editor is disabled.
It was working fine until the recent update to WordPress 4.7 – now it is adding some closing p tags, without opening them like </p> .

even tried this plugin but it disables br tags as well.

Any way of just disabling p tags not br tags in post content? I can’t find anything on the internet that says something about a solution.

2 Answers
2

You’d better never disable those actions (what you say). Instead, insert add_filter('the_content', 'MyFilter', 88 ); and create such function:

function MyFilter($content){
    $tags = array( 'p', 'span');
    ///////////////////////////////////////////////////
    ///////// HERE INSERT ANY OF BELOW CODE  //////////
    ///////////////////////////////////////////////////
    return $content;
}

======== METHOD 1 =========

$content= preg_replace( '#<(' . implode( '|', $tags) . ')(.*|)?>#si', '', $content);
$content= preg_replace( '#<\/(' . implode( '|', $tags) . ')>#si', '', $content);

======== METHOD 2 ======

foreach ($tags as $tag) {
    $content= preg_replace('#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi', '', $content);
}

======== METHOD 3 =========

DOM object (preferred): https://stackoverflow.com/a/31380542/2377343

Leave a Comment