Pergunta

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.

Foi útil?

Solução

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;      
}

Outras dicas

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.

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.

<?php

/**
 * 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(
        str_replace(
            array_keys( $replace ),
            array_values( $replace ),
            $pee
        )
    );

}

https://gist.github.com/fjarrett/ecddd0ed419bb853e390

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

$is_wpautop = ( $content !== fjarrett_unautop( $content ) );

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top