有没有人有很好的功能来去除 Wordpress 中的特定短代码


Does anyone have a good function for stripping out a specific shortcode in Wordpress?

我正在设置一个新的页面模板,该模板在一列中显示页面的图库(如果有的话),包含在一个独立的div中,向右浮动,所有其他内容向左浮动。我可以通过 get_post_gallery() 回显右侧的图库,现在我想将图库从 the_content() 中剥离出来。

我本质上希望找到的是一个与 strip_shortcodes() 完全相同的函数,但用于特定的简码。类似于strip_shortcode('gallery')或strip_shortcode('gallery', content())。有没有人为Wordpress编写这样的功能?

remove_shortcode("图库")作品栏,它在运行时会留下该死的短代码文本本身。我可以通过CSS隐藏图库或通过jQuery删除它,但我宁愿它首先不被输出。

要删除短代码或特定的短代码列表,您可以使用此代码。

global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* @global int $remove_shortcode
* @param type $shortcodes comma seprated string, array of shortcodes
* @return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
  global $remove_shortcode;
  if(empty($shortcodes)) return;
  if(!is_array($shortcodes)){
    $shortcodes = explode(',', $shortcodes);
  }
  foreach( $shortcodes as $shortcode ){
    $shortcode = trim($shortcode);
    if( shortcode_exists($shortcode) ){
        remove_shortcode($shortcode);
    }
    $remove_shortcode[$shortcode] = 1;
  }
  add_filter( 'the_excerpt', 'strip_shortcode' );
  add_filter( 'the_content', 'strip_shortcode' );    
}
function strip_shortcode( $content) {
  global $shortcode_tags, $remove_shortcode;
  $stack = $shortcode_tags;
  $shortcode_tags = $remove_shortcode;
  $content = strip_shortcodes($content);
  $shortcode_tags = $stack;
  return $content;
}
dot1_strip_shortcode( 'Test' );
dot1_strip_shortcode( 'Test, new_shortcode' );
dot1_strip_shortcode( array('Test', 'new_shortcode') );

接受单个逗号分隔的短代码字符串或短代码数组。