如何一劳永逸地消除帖子内容中的快捷码

时间:2012-07-17 作者:Diana

我更改了主题/插件,现在帖子内容仍然显示短代码。

关于隐藏它们有一些技巧,但每次我使用一些内容函数时,短代码就会再次出现。此外,我的短代码是“扩展类型”,例如[theshortcode id="34877"], [theshort size="large" "tiny"] 所以从数据库中删除它们的提示根本不起作用。

1 个回复
最合适的回答,由SO网友:Diana 整理而成

Please backup your database before trying

在当前主题中,打开functions.php 文件并添加下面的代码。在里面theshortcodeyouhate 通知您要删除的短代码,请注意,即使是扩展类型也可以很好地工作!

添加此代码后,按F5即可完成。

add_action (\'init\',\'remove_shortcode_from_db\'); //you can choose any other actions such wp_head etc

function remove_shortcode_from_db($shortcode = \'theshortcodeyouhate\') {

global $wpdb;

$posts = $wpdb->get_results("SELECT ID,post_title,post_content FROM
$wpdb->posts");
$regex =
\'/\\[(\\[?)(\'.$shortcode.\')\\b([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([
^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s\';

foreach($posts as $post) {
    $final = preg_replace($regex,\'\',$post->post_content);
    $change = $wpdb->update( $wpdb->posts, array(\'post_content\' =>
$final), array(\'ID\' => $post->ID));
}
}
感谢wp黑客邮件列表

结束

相关推荐

How to return loop contents

有时,我需要return 循环的输出(通常带有WP_Query 如本例所示)用于短代码或启用筛选器the_content.下面使用对象缓冲的代码可以工作,但我在其他地方读到过缓冲可能效率低下。我也看到过HEREDOC,但我不知道这在这里是如何工作的,除非我先将每个模板标记保存为变量(这似乎再次低效)。所以我的问题是,返回循环输出的最佳方法是什么?<?php if ( $cms_pl_pages->have_posts() ) : ob_start(); // start object