^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数do_shortcode()用法示例

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

Search content for shortcodes and filter shortcodes through their hooks.


描述

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

更新日志

Version 描述
2.5.0 Introduced.

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]'); ?>

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈