wordpress 函数:add-云顶国际

添加一个新的 shortcode(短代码)标签。

用法

参数

$tag
(string) (required) shortcode 短代码名称。
default: none

$func
(callable) (required) 短代码对应的 hook 函数,也就是找到 shortcode 标签时候处理的函数
default: none

返回值

实例

下面的例子是我写的一个插件,无需输入

  • html 标签,使用 shortcode 快速输入列表:

    [list type="order"]
    item-a
    item-b
    item-c
    [/list]

    内容中的每一行为列表中的一个元素,并且有个 type 的属性,默认为 0,如果为“order”,则为有序列表:

    function wpjam_list_shortcode_handler( $atts, $content='' ) {
        extract( shortcode_atts( array(
            'type' => '0'
        ), $atts ) );
        $lists = explode("\n", $content);
        $ouput = '';
        foreach($lists as $li){
            if(trim($li) != '') {
                $output .= "
  • {$li}
  • \n"; } } if($type=="order"){ $output = "
      \n".$output."
    \n"; }else{ $output = "
      \n".$output."
    \n"; } return $output; } add_shortcode( 'list', 'wpjam_list_shortcode_handler' );

    如果插件设计成一个 class,可以通过下面的方式调用:

    add_shortcode( 'list', array('myplugin', 'wpjam_list_shortcode_handler') );

    注解

    每个 shortcode(短代码)只能有一个对应的 hook 函数,这就意味着,如果另外一个插件,也有同样的短代码,就会覆盖你的短代码,或者你的覆盖它的,到底谁覆盖谁取决于你和他的插件那个后导入。

    短代码的属性名字在传递给对应处理函数之前会被转换成小写,但是属性值将不会改动。

    需要注意的是短代码的处理函数不应该直接输出,而应该返回文本用来替换短代码本身,如果直接输出将会导致不可预期的结果。短代码的处理函数和 的 filter 函数的行为类似,都是不会直接输出,而是返回文本。

    修改记录

    since: 2.5

    源文件

    wp-includes/shortcodes.php

             
    网站地图