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

wordpress函数add_shortcode()用法示例

add_shortcode( string $tag,  callable $func )

Add hook for shortcode tag.


描述

There can only be one hook for each shortcode. Which means that if another plugin has a similar shortcode, it will override yours or yours will override theirs depending on which order the plugins are included and/or ran.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

Simplest example of a shortcode tag using the API:

// [footag foo="bar"]
function footag_func( $atts ) {
    return "foo = {
        $atts[foo]
    }";
}
add_shortcode( 'footag', 'footag_func' );

Example with nice attribute defaults:

// [bartag foo="bar"]
function bartag_func( $atts ) {
    $args = shortcode_atts( array(
        'foo' => 'no foo',
        'baz' => 'default baz',
    ), $atts );

    return "foo = {$args['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );

Example with enclosed content:

// [baztag]content[/baztag]
function baztag_func( $atts, $content = '' ) {
    return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );

参数

$tag

(string)
(Required)
Shortcode tag to be searched in post content.

$func

(callable)
(Required)
Hook to run when shortcode is found.


源代码

File: wp-includes/shortcodes.php

function add_shortcode($tag, $func) {
	global $shortcode_tags;

	if ( '' == trim( $tag ) ) {
		$message = __( 'Invalid shortcode name: Empty name given.' );
		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
		return;
	}

	if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20=]@', $tag ) ) {
		/* translators: 1: shortcode name, 2: space separated list of reserved characters */
		$message = sprintf( __( 'Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s' ), $tag, '& / < > [ ] =' );
		_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
		return;
	}

	$shortcode_tags[ $tag ] = $func;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/functions.php:
    _doing_it_wrong()

Used By

  • wp-includes/class-wp-embed.php:
    WP_Embed::__construct()
  • wp-includes/class-wp-embed.php:
    WP_Embed::run_shortcode()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Examples

    Simplest example of a shortcode tag using the API: [footag foo="bar"]

    
    add_shortcode( 'footag', 'wpdocs_footag_func' );
    function wpdocs_footag_func( $atts ) {
    	return "foo = {$atts['foo']}";
    }
    

    Example with nice attribute defaults: [bartag foo="bar"]

    
    add_shortcode( 'bartag', 'wpdocs_bartag_func' );
    function wpdocs_bartag_func( $atts ) {
    	$atts = shortcode_atts( array(
    		'foo' => 'no foo',
    		'baz' => 'default baz'
    	), $atts, 'bartag' );
    
    	return "foo = {$atts['foo']}";
    }
    

    Example with enclosed content: [baztag]content[/baztag]

    
    add_shortcode( 'baztag', 'wpdocs_baztag_func' );
    function wpdocs_baztag_func( $atts, $content = "" ) {
    	return "content = $content";
    }
    

    If your plugin is designed as a class write as follows:

    
    add_shortcode( 'baztag', array( 'MyPlugin', 'wpdocs_baztag_func' ) );
    class MyPlugin {
    	public static function wpdocs_baztag_func( $atts, $content = "" ) {
    		return "content = $content";
    	}
    }
    

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

热门推荐

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

反馈