返回介绍

do_shortcode()

发布于 2017-09-10 22:19:26 字数 4756 浏览 1402 评论 0 收藏 0

do_shortcode( string $content,  bool $ignore_html = false )

Search content for shortcodes and filter shortcodes through their hooks.


description

If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.


参数

$content

(string) (Required) Content to search for shortcodes.

$ignore_html

(bool) (Optional) When true, shortcodes inside HTML elements will be skipped.

Default value: false


返回值

(string) Content with shortcodes filtered out.


源代码

File: wp-includes/shortcodes.php

function do_shortcode( $content, $ignore_html = false ) {
	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 );
	$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );

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

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

	$pattern = get_shortcode_regex( $tagnames );
	$content = preg_replace_callback( "/$pattern/", 'do_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.

More Information

If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues if a plugin is disabled as its shortcode will still show up in the post or content.


相关函数

Uses

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

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_media_shortcode()
  • wp-includes/class-wp-embed.php: WP_Embed::run_shortcode()
  • wp-includes/media.php: img_caption_shortcode()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 28You must log in to vote on the helpfulness of this note Contributed by Codex
    
    // Use shortcode in a PHP file (outside the post editor).
    echo do_shortcode( '' );
    
    
    // In case there is opening and closing shortcode.
    echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
    
    
    // Enable the use of shortcodes in text widgets.
    add_filter( 'widget_text', 'do_shortcode' );
    
    
    // Use shortcodes in form like Landing Page Template.
    echo do_shortcode( '[contact-form-7 title="quote"]' );
    
    
    // Store the short code in a variable.
    $var = do_shortcode( '' );
    echo $var;
    
  2. Square brackets are required to echo the shortcode, not just the name of the shortcode.

    echo do_shortcode( ' [ my_shortcode ] ' );

    This is not clear in the example.

    Example

    
    // Making your custom string parses shortcode
    $string = do_shortcode( $string );
    // If your string has a custom filter, add its tag name in an applicable add_filter function
    add_filter( 'my_string_filter_hook_tag_name', 'do_shortcode' );
    

    The most common use of the shortcode is the following

    <?php echo do_shortcode('[name_of_shortcode]'); ?>

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

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

发布评论

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