跳转到主要内容

基本简码

添加简码

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


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;
}

删除短代码

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


remove_shortcode(
    string $tag
);

在尝试删除之前,请确保已注册短代码。为add_action()指定更高的优先级编号 或挂钩到稍后运行的操作挂钩。

检查短代码是否存在

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