$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>import - How can I prevent the WordPress Importer from munging double-newline paragraph breaks to a single newline?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

import - How can I prevent the WordPress Importer from munging double-newline paragraph breaks to a single newline?

matteradmin9PV0评论

When you import posts with the standard WP Importer plugin, most users are going to find that it changes the standard paragraph break sequence of \n\n to a single \n. When it comes time to display the post later, this will prevent wpautop() from wrapping consecutive paragraphs in <p>...</p> tags, instead replacing the break between them with a <br />.

When you import posts with the standard WP Importer plugin, most users are going to find that it changes the standard paragraph break sequence of \n\n to a single \n. When it comes time to display the post later, this will prevent wpautop() from wrapping consecutive paragraphs in <p>...</p> tags, instead replacing the break between them with a <br />.

Share Improve this question asked Dec 29, 2018 at 0:21 scott8035scott8035 2302 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I banged my head on the wall for hours before I realized there was a simple solution I could use to pre-process the import XML file prior to running WP Importer. The solution is to just run the file through a home-grown filter program that runs wpautop() on the content before it has a chance to get munged by the plugin.

Since getting the content blocks out of the input XML is a little tedious, I decided to share my code with the community to kick-start your next import. The code and a lengthier explanation are at "WordPress import with wpautop". I'll include the PHP code here for preservation within StackExchange:

$accum  = 0;
$buffer = '';

while ( $line = fgets( STDIN ) ) {
    $line = preg_replace( '/\r\n/', "\n", $line );
    $line = preg_replace( '/\r/',   "\n", $line );

    $start = false;
    $end   = false;
    if ( preg_match( '/^\s*<content:encoded><!\[CDATA\[/', $line ) ) {
        $line = preg_replace( '/^\s*<content:encoded><!\[CDATA\[/', '', $line ); 
        $start = true; 
    } 
    if ( preg_match( '/\]\]><\/content:encoded>\s*$/i', $line ) ) {
        $line = preg_replace( '/\]\]><\/content:encoded>\s*$/i', '', $line );
        $end = true;
    }

    if ( $start && $end ) {
        echo $line;
    } elseif ( $start ) {
        $accum = true;
        $buffer = $line;
    } elseif ( $end ) {
        $accum = false;
        $buffer .= $line;
        echo '<content:encoded><![CDATA[' . wpautop( $buffer ) . ']]></content:encoded>';
    } else {
        if ( $accum ) {
            $buffer .= $line;
        } else {
            echo $line;
        }
    }
}

exit(0);

This technique relies on being able to run wpautop() from inside a plain ol' command line program. See "Using WordPress in a non-interactive 'batch' CLI process" for details on how to do that.

Post a comment

comment list (0)

  1. No comments so far