Wordpress remove shortcode from content

别等时光非礼了梦想. 提交于 2020-01-01 17:26:12

问题


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); 

回答1:


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:

remove_shortcode('gallery');
return $content;

Or just put

 remove_shortcode('gallery');

In your functions.php file. The previous poster suggested including the [ ]'s, which I guess is wrong.




回答2:


I know this is a relative old question, but the strip_shortcodes function does work!

global $post;
echo strip_shortcodes($post->post_content);

Easiest way if you ask me..




回答3:


I think you should use sub string replacement like this:

function remove_gallery($content) {
    return str_replace('[gallery]', '', $content);
}
add_filter( 'the_content', 'remove_gallery', 6); 

Bear in mind, this method does not come with good performance.

update: You can unregister the shotcode in function.php by adding code:

remove_shortcode('[gallery]');



回答4:


An old question, but after some digging and a combination of answers this worked for me:

<?php $content = get_the_content();

echo strip_shortcodes($content);?>

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.



来源:https://stackoverflow.com/questions/8547525/wordpress-remove-shortcode-from-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!