隐藏失效的 WordPress 短代码

WordPress评论285阅读模式

很多 WordPress 主题或插件都集成了独有的短代码,从则实现一些功能,但有朝一日换主题或停用后,短代码失效,只显示一段代码,可以通过下面的方法隐藏这些失效的 WordPress 短代码。

section

例如原添加短代码代码为:

add_shortcode('my-shortcode', 'my_shortcode_function');

想隐藏这个短代码,将下面的代码添加到当前主题函数模板functions.php中即可。

add_shortcode( 'my-shortcode', '__return_false' );

其中的my-shortcode为原短代码名称。

section

如果想删除这些短代码,在众多文章中逐个打开查找,太麻烦,可以用下面的代码批量搜索哪些文章添加了某个短代码。

将代码添加到当前主题函数模板functions.php中:

function zm_find_shortcode( $atts, $content=null ) { 
	ob_start();
	extract( shortcode_atts( array(
		'find' => '',
		), $atts ) );

	$string = $atts['find'];
	$args = array(
		's' => $string,
		);

	$the_query = new WP_Query( $args );
	if ( $the_query->have_posts() ) {
		echo '<ul>';
		while ( $the_query->have_posts() ) {
			$the_query->the_post(); ?>
			<li><a href="<?php  the_permalink() ?>"><?php the_title(); ?></a></li>
			<?php
		}
		echo '</ul>';
	} else {
		echo "Sorry no posts found"; 
	}
	wp_reset_postdata();
	return ob_get_clean();
}
add_shortcode( 'shortcodefinder', 'zm_find_shortcode' );

发表一篇文章,并添加短代码:

[shortcodefinder find='短代码名称']

之后会在这篇文章中列出所有添加短代码的文章。

这段搜索短代码文章的方法还有其它用处,简单改一下,可以调用指定的文章,比如调用添加过下载按钮短代码的文章等。

section

还可以安装短代码查找插件:Shortcodes Finder

通过插件不仅可以找到有短代码的文章,还可以查看短代码的源代码位置,并可以编辑。

 

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证