Cyb*_*kie 5 php wordpress shortcode
我想[gallery]在我的博文中删除短代码.我发现的唯一解决方案是我添加到我的函数中的过滤器.
function remove_gallery($content) {
if ( is_single() ) {
$content = strip_shortcodes( $content );
}
return $content;
}
add_filter('the_content', 'remove_gallery');
Run Code Online (Sandbox Code Playgroud)
它删除了所有短代码,包括[caption]我需要的图像.如何指定要排除或包含的单个短代码?
hak*_*kre 13
要仅删除库短代码,请注册一个返回空字符串的回调函数:
add_shortcode('gallery', '__return_false');
Run Code Online (Sandbox Code Playgroud)
但这只适用于回调.要静态地执行此操作,您可以暂时更改wordpress的全局状态以欺骗它:
/**
* @param string $code name of the shortcode
* @param string $content
* @return string content with shortcode striped
*/
function strip_shortcode($code, $content)
{
global $shortcode_tags;
$stack = $shortcode_tags;
$shortcode_tags = array($code => 1);
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
Run Code Online (Sandbox Code Playgroud)
用法:
$content = strip_shortcode('gallery', $content);
Run Code Online (Sandbox Code Playgroud)