我想更改wordpress wp_posts表上的短代码。在post_contents字段中,我有一些html代码,其中包含很少的短代码:
[PFltr "this text is variable, changes on each occurrence"]
我想改变这个:
[scode option="1"]this text is variable changes on each occurrence[/scode]
是否有任何mysql更新查询,可能使用正则表达式来执行此操作?
答案 0 :(得分:0)
答案 1 :(得分:0)
这段代码会做你的事:
add_action('template_redirect', 'update_my_content_shortcodes');
function update_my_content_shortcodes() {
global $wpdb;
$posts = $wpdb->get_results(
"SELECT ID, post_content
FROM $wpdb->posts
WHERE post_content LIKE '%[PFltr%'
");
foreach ($posts as $p) {
$updated_content = preg_replace('~\[PFltr\s*\"([^\"]*)\"\]~', '[scode option="1"]$1[/scode]', $p->post_content);
wp_update_post(array(
'ID' => $p->ID,
'post_content' => $updated_content
));
}
}
一旦您在前端打开WordPress,它就会执行。 请注意,它将在每个页面加载时执行,因此您可能只想执行一次,或在某些条件下执行它。