据我所知,MySQL没有太多的regexp替换功能——更不用说它非常笨拙的regexp语法了。因此,这在php级别上是最容易做到的。首先获取所有具有短代码的帖子:
$posts = $wpdb->get_results("
SELECT ID, post_content
FROM $wpdb->posts
WHERE post_content LIKE \'%[zdvideo]%\'
");
然后循环查看结果:
foreach ($posts as $post) {
$post->post_content = preg_replace(
"/\\\\[zdvideo\\\\](.+?)\\\\[\\\\/zdvideo\\\\]/",
"$1", # $1 holds the url... format as needed
$post->post_content);
# Be sure to verify on a few posts before actually saving...
# var_dump($post->post_content);
$wpdb->query("
UPDATE $wdpb->posts
SET post_content = \'" . $wpdb->escape($post->post_content) . "\'
WHERE ID = " . intval($post->ID)
);
}