WordPress 从内容中删除短代码
是否可以在执行 the_content()
之前从内容中删除图库短代码?我搜索了 codex 并找到了 remove_shortcode( $tag )
但他们没有显示任何示例。
我尝试添加功能,
function remove_gallery($content) {
$content .= remove_shortcode('[gallery]');
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
但不起作用。
更新:
我能够使用下面的代码取消注册短代码,但它也会删除内容
function remove_gallery($content) {
return remove_shortcode('gallery', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
Is it possible to remove the gallery shortcode from the content before when the_content()
is executed? I searched codex and found remove_shortcode( $tag )
but they show no examples.
I tried adding to functions
function remove_gallery($content) {
$content .= remove_shortcode('[gallery]');
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
It doesnt work..
Update:
I was able to unregister the shortcode using the code below, but it also removes the content
function remove_gallery($content) {
return remove_shortcode('gallery', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这是一个相对古老的问题,但 strip_shortcodes 函数确实有效!
如果你问我最简单的方法..
I know this is a relative old question, but the strip_shortcodes function does work!
Easiest way if you ask me..
奇怪的。 remove_shortcode(codex 链接)不接受第二个参数。
您将返回remove_shortcode 函数的 true 或 false 返回,而不是删除了短代码的内容。
在您的函数的第二个版本中尝试类似的操作:
或者只是放入
您的functions.php 文件中。上一张海报建议包含 [ ],我认为这是错误的。
Strange. remove_shortcode (codex link) doesn't take a second argument.
You're returning either the true or false return of the remove_shortcode function, not the content with the shortcode removed.
Try either something like this in that second version of your function up there:
Or just put
In your functions.php file. The previous poster suggested including the [ ]'s, which I guess is wrong.
我认为您应该像这样使用子字符串替换:
请记住,此方法不会带来良好的性能。
更新:您可以通过添加代码来注销function.php中的shotcode:
I think you should use sub string replacement like this:
Bear in mind, this method does not come with good performance.
update: You can unregister the shotcode in function.php by adding code:
这是一个老问题,但经过一些挖掘和答案组合后,这对我有用:
我只是插入画廊,我想将其删除并单独显示。显然,如果您想删除特定的短代码,这可能不适合您。
An old question, but after some digging and a combination of answers this worked for me:
I was only inserting galleries, which I wanted to remove and display separately. Obviously if you want to remove a specific shortcode this might not be the solution for you.
我知道这是一篇旧帖子,但以防万一有人正在寻找一种方法来有选择地从 WordPress 中的帖子内容(或您拥有的任何字符串)中删除单个短代码或短代码列表,例如,您不想使用 strip_shortcodes 删除所有短代码,您只想删除 [abc id='blah'] 短代码或其他内容:
在这种情况下 $content 是包含短代码的字符串,您可以在数组中列出一个或多个短代码名称(替换“abc”、“shortcode2”)。即使您要删除的短代码未注册,这仍然有效,例如,如果以前的插件使用了 [pluginshortcode],您可以删除它们。
I know this is an old post but just in case someone is looking for a way to selectively remove a single shortcode or list of shortcodes from post content (or whatever string you've got) in WordPress, ex, you don't want to use strip_shortcodes to remove ALL shortcodes, you just want to remove the [abc id='blah'] shortcode or something:
In this case $content is the string containing the short codes, and you can list one or more shortcode names in the array (replacing 'abc', 'shortcode2'). This still works even if the shortcode(s) you want to remove are not registered, for example if a former plugin used [pluginshortcode] you can remove those.