添加新的短代码。
原型
add_shortcode( string $tag, callable $callback )
描述
应注意通过前缀或其他方式确保添加的短代码标记是唯一的,并且不会与其他已添加的短代码标记冲突。如果出现重复的标记,则最后加载的标记将优先。
参数
$tag
(string)
(Required)
要在帖子内容中搜索的短代码标签。
$callback
(callable)
(Required)
找到短代码时运行的回调函数。默认情况下,每个短代码回调都会传递三个参数,包括一个属性数组($ atts),短代码内容,如果没有设置,则为null($ content),最后是短代码标签本身($ shortcode_tag)。
源文件
路径:wp-includes/shortcodes.php
<?php
...
function add_shortcode( $tag, $callback ) {
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 ] = $callback;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/add_shortcode/