I have a problem with WP that need your help. Some of my posts have content that was applied "wp_autop" filters already. This filter turned all double break lines into <p> tag. I want to the opposite thing: turn all <p> tag into double break lines.

Do you have any suggestions? Thank you.

  • There's also a few plugins (like Disable WPautop) that will take care of this for you. – Andrew Jan 20 '12 at 17:56

I just ran into this situation. Here is a function I used to undo wpautop. I might be missing something, but this is a good start:

function reverse_wpautop($s)
    //remove any new lines already in there
    $s = str_replace("\n", "", $s);

    //remove all <p>
    $s = str_replace("<p>", "", $s);

    //replace <br /> with \n
    $s = str_replace(array("<br />", "<br>", "<br/>"), "\n", $s);

    //replace </p> with \n\n
    $s = str_replace("</p>", "\n\n", $s);       

    return $s;      
| improve this answer | |

The filter write not in the database, filter on frontend, when view the post or other content. You deactivate the filter and write a own filter for set your custom markup.

| improve this answer | |
  • It might be he somehow added autop'ed content into the DB, that's what I understood from it. – Not Available Dec 20 '10 at 14:28
  • Xeross is right. The autop'ed content was added into DB already. And I want to clean up it to un-autop version. – Anh Tran Dec 21 '10 at 3:38
  • 1
    Maybe you can filter with sql all <p></p> and <br /> in the datebase. A esay way to do this is the plugin Search & Replace. – bueltge Dec 21 '10 at 23:51
  • bueltge was referring to the Search and Replace (Wordpress Plugin). You can use it to easily search and replace in your database. – hakre Feb 5 '11 at 23:39

I also needed a way to do this but wasn't happy with any existing solutions so decided to make one. Hope it helps someone.


 * Replaces paragraph elements with double line-breaks.
 * This is the inverse behavior of the wpautop() function
 * found in WordPress which converts double line-breaks to
 * paragraphs. Handy when you want to undo whatever it did.
 * @see    wpautop()
 * @param  string $pee
 * @param  bool   $br (optional)
 * @return string
function fjarrett_unautop( $pee, $br = true ) {

    // Match plain <p> tags and their contents (ignore <p> tags with attributes)
    $matches = preg_match_all( '/<(p+)*(?:>(.*)<\/\1>|\s+\/>)/m', $pee, $pees );

    if ( ! $matches ) {

        return $pee;


    $replace = array( "\n" => '', "\r" => '' );

    if ( $br ) {

        $replace['<br>']   = "\r\n";
        $replace['<br/>']  = "\r\n";
        $replace['<br />'] = "\r\n";


    foreach ( $pees[2] as $i => $tinkle ) {

        $replace[ $pees[0][ $i ] ] = $tinkle . "\r\n\r\n";


    return rtrim(
            array_keys( $replace ),
            array_values( $replace ),



Bonus: You could also potentially use this to determine if content has been altered by wpautop.

$is_wpautop = ( $content !== fjarrett_unautop( $content ) );
| improve this answer | |

There's a built in function in WordPress, but it's JavaScript only triggered when you switch from Visual to HTML in the post editor (and it's really buggy if html is actually used though). If its just normal post content (not html code) then you can just edit each post, switch the editor back and forth, then save.

This is probably the most labor intensive, but safest, way.

| improve this answer | |

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.