WordPress函数文档add_site_option()
注册添加一个短标签执行钩子 描述 Adds a hook for a shortcode tag. 用法 &l…
注册添加一个短标签执行钩子
描述
Adds a hook for a shortcode tag.
用法
<?php add_shortcode( $tag , $func ); ?>
参数
$tag
(string) (必填) Shortcode tag to be searched in post content
默认值: None
$func
(callable) (必填) Hook to run when shortcode is found
默认值: None
返回值
(none)
示例
Simplest example of a shortcode tag using the API:
[footag foo=”bar”]
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function footag_func( $atts ) {
return “foo = {$atts[‘foo’]}”;
}
add_shortcode( ‘footag’, ‘footag_func’ );
|
Example with nice attribute defaults:
[bartag foo=”bar”]
1
2
3
4
5
6
7
8
9
10
11
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function bartag_func( $atts ) {
$atts = shortcode_atts( array(
‘foo’ => ‘no foo’,
‘baz’ => ‘default baz’
), $atts, ‘bartag’ );
return “foo = {$atts[‘foo’]}”;
}
add_shortcode( ‘bartag’, ‘bartag_func’ );
|
Example with enclosed content:
[baztag]content[/baztag]
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function baztag_func( $atts, $content = “” ) {
return “content = $content”;
}
add_shortcode( ‘baztag’, ‘baztag_func’ );
|
If your plugin is designed as a class write as follows:
1
2
3
4
5
6
7
8
9
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
class MyPlugin {
public static function baztag_func( $atts, $content = “” ) {
return “content = $content”;
}
}
add_shortcode( ‘baztag’, array( ‘MyPlugin’, ‘baztag_func’ ) );
|
注意
- The shortcode callback will be passed three arguments: the shortcode attributes, the shortcode content (if any), and the name of the shortcode.
- 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.
- Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.
- Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way 过滤器 functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.
历史
添加于 版本: 2.5
源文件
add_shortcode() 函数的代码位于 wp-includes/shortcodes.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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.
*
* 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’ );
*
* @since 2.5.0
*
* @global array $shortcode_tags
*
* @param string $tag Shortcode tag to be searched in post content.
* @param callable $func Hook to run when shortcode is found.
*/
function add_shortcode($tag, $func) {
global $shortcode_tags;
$shortcode_tags[ $tag ] = $func;
}
|
相关
Shortcode API
- Function(函数): do_shortcode()
- Function(函数): add_shortcode()
- Function(函数): remove_shortcode()
- Function(函数): remove_all_shortcodes()
- Function(函数): shortcode_atts()
- Function(函数): strip_shortcodes()
- Function(函数): shortcode_exists()
- Function(函数): has_shortcode()
- Function(函数): get_shortcode_regex()
- Function(函数): wp_audio_shortcode()
- Function(函数): wp_video_shortcode()
- Filter(过滤器): no_texturize_shortcodes
- 原文:http://codex.wordpress.org/Function_Reference/add_shortcode
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!