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
    Commented Dec 10, 2016 at 6:08
  • that is only saying about 'wpautop', which is the problem I specified here
    – vyperlook
    Commented Jan 14, 2017 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
    Commented Jan 15, 2017 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
    Commented Jan 15, 2017 at 7:28
  • @TomJNowell yes, I guess so.
    – vyperlook
    Commented Jan 15, 2017 at 9:41

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

  • 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
    – vyperlook
    Commented Jan 15, 2017 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
    – vyperlook
    Commented Jan 15, 2017 at 15:15
  • I've updated answer, using another method. see that.
    – T.Todua
    Commented Jan 15, 2017 at 15:16
  • wonderful! method 2 seems to work properly. will do a little more testing and then choose your answer. thanks a lot!
    – vyperlook
    Commented Jan 15, 2017 at 15:36
  • I did, but it doesn't work for me. why, is it better than method 2?
    – vyperlook
    Commented Jan 15, 2017 at 15:52

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
    Commented Jan 15, 2017 at 14:06
  • added your code but the p tags are still added
    – vyperlook
    Commented Jan 15, 2017 at 15:14

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.