3

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.

  • This might be helpfull stackoverflow.com/questions/11248628/… – Rishabh Dec 10 '16 at 6:08
  • that is only saying about 'wpautop', which is the problem I specified here – whitelord Jan 14 '17 at 22:15
  • You appear to have 2 distinct problems here, 1: how do I stop WP creating paragraph tags? 2: How do I add br tags to the whitelist? It looks like you've already solved problem 1, and need help solving problem 2. Is this correct? – Tom J Nowell Jan 15 '17 at 1:01
  • @whiteord - Is that happening only while added the content or only by adding shortcode? I too experienced it but only when I used shortcodes. – Maqk Jan 15 '17 at 7:28
  • @TomJNowell yes, I guess so. – whitelord Jan 15 '17 at 9:41
6
+50

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

  • thanks, but when using your function not even audio player will show. it removes quite a lot. not sure how to add all the tags to the list. even my divs won't show – whitelord Jan 15 '17 at 15:11
  • even after adding them, and span it won't work. all the content si broke, and no embed or div will show – whitelord Jan 15 '17 at 15:15
  • I've updated answer, using another method. see that. – T.Todua Jan 15 '17 at 15:16
  • wonderful! method 2 seems to work properly. will do a little more testing and then choose your answer. thanks a lot! – whitelord Jan 15 '17 at 15:36
  • I did, but it doesn't work for me. why, is it better than method 2? – whitelord Jan 15 '17 at 15:52
0

The "the_content" filter is used to filter the content of the post where filter function in your_prefix_ptagfix forces to find the <p> tags before and after the shortcodes opening tags.

Add The following code to your functions file and let me know if that helps.

add_action( 'after_setup_theme', 'your_prefix_theme_setup' );
function your_prefix_theme_setup(){
    add_filter( 'the_content', 'your_prefix_ptagfix' );

}

function your_prefix_ptagfix($content){   
    $array = array (
        '<p>[' => '[', 
        ']</p>' => ']',
    );

    $content = strtr($content, $array);
    return $content;
}
  • Can you please explain in an edit to your answer why your answer should solve the OPs problem? – kaiser Jan 15 '17 at 14:06
  • added your code but the p tags are still added – whitelord Jan 15 '17 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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