WordPress函数:strip_shortcodes 删除短代码

江河/ 2023年07月15日/ WordPress/ 浏览 1300

函数原型:


strip_shortcodes( string $content ): string


从给定内容中删除所有短代码标记。


参数说明:


$content,要删除短代码标记的内容。


函数源码:


function strip_shortcodes( $content ) {
	global $shortcode_tags;

	if ( false === strpos( $content, '[' ) ) {
		return $content;
	}

	if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
		return $content;
	}

	// Find all registered tag names in $content.
	preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );

	$tags_to_remove = array_keys( $shortcode_tags );

	$tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );

	$tagnames = array_intersect( $tags_to_remove, $matches[1] );

	if ( empty( $tagnames ) ) {
		return $content;
	}

	$content = do_shortcodes_in_html_tags( $content, true, $tagnames );

	$pattern = get_shortcode_regex( $tagnames );
	$content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );

	// Always restore square braces so we don't break things like <!--[if IE ]>.
	$content = unescape_invalid_shortcodes( $content );

	return $content;
}


包含钩子:


apply_filters( 'strip_shortcodes_tagnames', array $tags_to_remove, string $content )


使用举例:


function wpdocs_remove_shortcode_from_index( $content ) {
	if ( is_home() ) {
		$content = strip_shortcodes( $content );
	}
	return $content;
}
add_filter( 'the_content', 'wpdocs_remove_shortcode_from_index' );


发表评论

暂无评论,抢个沙发...

客服 工单