admin管理员组文章数量:1130349
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 />.
1 Answer
Reset to default 1I 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.
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 />.
1 Answer
Reset to default 1I 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.
本文标签:
版权声明:本文标题:import - How can I prevent the WordPress Importer from munging double-newline paragraph breaks to a single newline? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749061313a2310146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论