基本短码

添加短代码

可以使用短代码API添加您自己的短代码。该过程涉及使用add_shortcode()将回调$func注册到短代码$tag

add_shortcode(
    string $tag,
    callable $func
);

[wporg]是您的新短代码。使用短代码将触发wporg_shortcode回调函数。

add_shortcode('wporg', 'wporg_shortcode');
function wporg_shortcode( $atts = [], $content = null) {
    // do something to $content
    // always return
    return $content;
}

 

删除短代码

可以使用短代码API删除短代码。该过程涉及使用remove_shortcode()删除已注册的$tag

remove_shortcode(
    string $tag
);

在尝试删除之前,请确保已注册短代码。为add_shortcode()指定更高的优先级,或将其挂接到靠后运行的动作钩子中。

 

检查是否存在短代码

要检查是否已注册短代码,请使用shortcode_exists()