未经测试,但我相信您可以这样做:
add_action( \'init\', function() {
// since were editing content, we want to be very very careful
// well do a dry run output first to make sure were doing what we want
$dryrun = true;
// Regex pattern finds anything between the X and Y bounds.
$pattern = \'/(XXXX)(.+?)(YYYY)/gms\';
// Get and cycle through ALL published `posts`
$all_posts = get_posts(\'posts_per_page=-1\');
foreach ($all_posts as $a_post) {
// replace the pattern with our new content.
// If the content is to be preserved, we could use $2 in the second arg
$new_content = preg_replace($pattern, "New content here", $a_post->post_content);
// if somthing was replaced, lets continue
if ( $new_content != $a_post->post_content ) {
// display what _would_ be changing
if ( $dryrun ) {
echo "<div>update post #{$a_post->ID} to:</div>";
echo "<pre>{$new_content}</pre>";
} else {
// update the post_content with our new content
wp_update_post(array(
\'ID\' => $a_post->ID,
\'post_content\' => $new_content,
));
}
} else {
// nothing was changed, if dry run, just print the notice.
if ( $dryrun ) {
echo "<div>nothing to update in post #{$a_post->ID}</div>";
}
}
}
});
即使这段代码有一次试运行,
always 在像这样进行大规模修改之前,请备份数据库内容并进行验证,否则很快就会出现可怕的错误。