返回介绍

strip_shortcodes()

发布于 2017-09-11 10:30:00 字数 3430 浏览 1001 评论 0 收藏 0

strip_shortcodes( string $content )

Remove all shortcode tags from the given content.


description


参数

$content

(string) (Required) Content to remove shortcode tags.


返回值

(string) Content without shortcode tags.


源代码

File: wp-includes/shortcodes.php

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

	/**
	 * Filters the list of shortcode tags to remove from the content.
	 *
	 * @since 4.7.0
	 *
	 * @param array  $tag_array Array of shortcode tags to remove.
	 * @param string $content   Content shortcodes are being removed from.
	 */
	$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;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/shortcodes.php: strip_shortcodes_tagnames
  • wp-includes/shortcodes.php: do_shortcodes_in_html_tags()
  • wp-includes/shortcodes.php: unescape_invalid_shortcodes()
  • wp-includes/shortcodes.php: get_shortcode_regex()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/formatting.php: wp_trim_excerpt()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Strip shortcodes on home page but not on single or archive pages
    When viewing posts ‘home’ page, strip all shortcodes, but on other pages, such as single.php, do not strip the shortcodes.

    
    /**
     * Remove shortcodes if we are on the home page
     *
     * @param string $content Post content.
     * @return string (Maybe) modified post 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' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文