add_shortcode()注册添加一个短标签执行钩子

目录

描述

Adds a hook for a shortcode tag.

add_shortcode() 描述

用法

<?php add_shortcode$tag $func ); ?>

add_shortcode() 用法

参数

$tag

(string) (必填) Shortcode tag to be searched in post content

默认值: None

$func

(callable) (必填) Hook to run when shortcode is found

默认值: None

add_shortcode() 参数

返回值

(none)

add_shortcode() 返回值

示例

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

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

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

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

add_shortcode() 示例

注意

  • 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.

add_shortcode() 注意

历史

添加于 版本: 2.5

add_shortcode() 历史

源文件

add_shortcode() 函数的代码位于 wp-includes/shortcodes.php.

add_shortcode() 源文件

相关