城市简码_如何从WordPress帖子中查找和删除未使用的简码

城市简码

Shortcodes are great, but they are not always the best way. One disadvantage of using a plugin or theme that relies on shortcodes is that when you switch a theme or deactivate the plugin, they will leave behind shordcode tags in your posts which will look strange to your readers. In this article, we will show how to find and remove unused shortcodes from your WordPress posts and pages.

短代码很棒,但并非总是最好的方法。 使用依赖于短代码的插件或主题的一个缺点是,当您切换主题或停用插件时,它们将在您的帖子中留下shordcode标记,这会使读者感到奇怪。 在本文中,我们将展示如何从WordPress帖子和页面中查找和删除未使用的短代码。

简码不好吗? (Are Shortcodes Bad?)

No, absolutely not. Shortcodes are not bad, but overusing them can be problematic. For example, we use Compact Archives plugin which provides a shortcode and a template tag. We have the shortcode only on our archives page, so if we ever deactivate that plugin, then there is just one page we need to remove the shortcode from.

,绝对不是。 短代码还不错,但是过度使用它们可能会出现问题。 例如,我们使用Compact Archives插件,该插件提供了简码和模板标签。 我们仅在存档页面上有简码,因此,如果我们停用该插件,则只需要将其删除其中的一页。

On the other hand, there are plugins and themes that provide shortcodes to create common style elements like buttons, tables, columns, etc. Some ad management plugins also use shortcodes. Now if a user has used these shortcodes in many posts, then it becomes very difficult for the user to remove the shortcode from all posts and pages.

另一方面,有些插件和主题提供了用于创建常见样式元素(如按钮,表格,列等)的简码。一些广告管理插件也使用简码。 现在,如果用户在许多帖子中使用了这些短代码,则用户很难从所有帖子和页面中删除该短代码。

This is why we recommend our users to not rely on themes or plugins which require you to add shortcodes into many posts. You should always try to find a better alternative if you can, or contact the theme or plugin author. They might tell you a better way to get the same functionality without using too many shortcodes in posts or pages.

这就是为什么我们建议用户不要依赖主题或插件,而主题或插件要求您在许多帖子中添加短代码。 如果可以,您应该始终尝试找到更好的选择,或者与主题或插件作者联系。 他们可能会告诉您一种更好的方式来获得相同的功能,而无需在帖子或页面中使用太多的短代码。

For those still wondering, if you have an inactive shortcode on your site, then it will look like this in the middile of your content:

对于那些仍然想知道的人,如果您的站点上有一个不活动的简码,那么在内容的中间部分看起来像这样:

[some-random-shortcode]

[some-random-shortcode]

In order to remove unused shortcodes from your posts and pages, you need to first find them.

为了从帖子和页面中删除未使用的简码,您需要先找到它们。

查找包含特定简码的所有帖子 (Find All Posts Containing a Particular Shortcode)

We will try the simplest approach to find the shortcode inside post content. Simply copy and paste the following code in a site-specific plugin or your theme’s functions.php file:

我们将尝试最简单的方法来在帖子内容中查找简码。 只需将以下代码复制并粘贴到特定站点的插件或主题的functions.php文件中:


function wpb_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', 'wpb_find_shortcode'); 

In this code, we have created a shortcode (how ironic is that?). The shortcode runs a function to execute a custom WordPress Query. In this query, we are using the default WordPress search feature to find the shortcode and then list all posts found with that specific shortcode.

在此代码中,我们创建了一个简码( 这具有讽刺意味吗? )。 简码运行一个函数来执行自定义WordPress查询。 在此查询中,我们使用默认的WordPress搜索功能查找短代码,然后列出使用该特定短代码找到的所有帖子。

To use this, you need to create a new WordPress post or page and paste this shortcode inside it:

要使用此功能,您需要创建一个新的WordPress帖子或页面并将此短代码粘贴到其中:

[shortcodefinder find='myshortcode']

[shortcodefinder find='myshortcode']

Replace myshortcode with the shortcode tag you are looking for. Save your post or page as a draft and then preview it. This will allow you to see a list of all posts containing the shortcode tag you searched for.

myshortcode替换为myshortcode标记。 将您的帖子或页面另存为草稿,然后进行预览。 这将使您可以查看包含所搜索的shortcode标签的所有帖子的列表。

如何在WordPress中删除未使用的简码 (How to Remove Unused Shortcodes in WordPress)

Unfortunately, the best way to remove unused shortcodes from your posts is by manually editing each post containing the shortcode. In the method described above, we showed you how to get a list of posts containing a particular shortcode. Hopefully, this will save you some time. Once you have the list, then you can go through the posts one by one and remove the shortcode.

不幸的是,从帖子中删除未使用的简码的最好方法是手动编辑每个包含简码的帖子。 在上述方法中,我们向您展示了如何获取包含特定短代码的帖子列表。 希望这可以节省您一些时间。 有了列表后,就可以一个接一个地浏览帖子并删除简码。

Alternatively if you don’t want to go edit your posts one by one, then there is a quick work around that would effectively hide the shortcode from appearing into your content. Simply paste the following code in a site-specific plugin or your theme’s functions.php file:

另外,如果您不想一个接一个地编辑帖子,则可以通过快速解决方法来有效地隐藏短代码,使其不会出现在您的内容中。 只需将以下代码粘贴到特定于站点的插件或主题的functions.php文件中:


add_shortcode( 'shortcodetag', '__return_false' );

You would need to replace shortcodetag with the shortcode appearing in your posts or the shortcode you want to hide.

您需要将shortcodetag替换为帖子中显示的短代码或您要隐藏的短代码。

Basically the code above will add the shortcode and make it show nothing. This way your shortcode will be parsed as any other registered shortcode would, but without showing anything in the output. If there are multiple unused shortcodes in your posts, then you can reuse this code by just replacing the shortcodetag with the shortcode you want to hide.

基本上,上面的代码将添加简码,使其不显示任何内容。 这样,您的简码将像任何其他已注册的简码一样被解析,但不会在输出中显示任何内容。 如果您的帖子中有多个未使用的简码,则可以通过将shortcodetag替换为要隐藏的简码来重复使用此代码。

We hope this article helped you find and remove unused shortcodes from your WordPress posts or pages. For questions and feedback please leave a comment below.

我们希望本文能帮助您从WordPress帖子或页面中查找和删除未使用的简码。 对于问题和反馈,请在下面留下评论。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-find-and-remove-unused-shortcodes-from-wordpress-posts/

城市简码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值