Woo主题的框架很灵活,想其主题选项(theme options)中添加自定义的选项可以通过插件完成,本文演示如何添加选项。

Woo主题选项定义方法

Woo主题选项定义在includes/theme-options.php中,下载一款免费woo主题,打开这个文件可以看到如下结构。

$options = array();
$options[] = array("name" => "General Settings",
    "type" => "heading",
    "icon" => "general");
$options[] = array("name" => "Custom Logo",
    "desc" => "Upload a logo for your theme, or specify an image URL directly.",
    "id" => $shortname . "_logo",
    "std" => "",
    "type" => "upload");

虽然主题选项看起来是选项卡式的,好像存在父子关系,但代码中无论是选项卡标题还是具体的选项,都是平级关系,只是具有不同的type,$options是一个简单的二维数组。

添加一个新的选项卡

添加一个名为Custom Settings的选项卡,内置一个简单的文本框选项

function woo_options_add($options){
    $shortname = 'woo';
    $options[] = array( "name" => "Custom Settings",
                "type" => "heading",
                "icon" => "general" );
    $options[] = array(
        'desc' => 'Input your tracking code',
        'id' => $shortname . '_trackingcode',
        'std' => '',
        'type' => 'textarea'
    );
    
    return $options;
}

该选项卡会添加到所有选项卡末尾,若要添加到开头,可以这样

function woo_options_add($options){
    $shortname = 'woo';
    $array[0] = array( "name" => "Custom Settings",
                "type" => "heading",
                "icon" => "general" );
    $array[1] = array(
        'desc' => 'Input your tracking code',
        'id' => $shortname . '_trackingcode',
        'std' => '',
        'type' => 'textarea'
    );
    array_unshift( $options, $array[1]);
    array_unshift( $options, $array[0]);
    
    return $options;
}

向选项卡中插入自定义选项

现在要向General Settings中添加一个选项,方法如下

function woo_options_add($options){   
    $shortname = 'woo';
    $array = array();
    while(list($key, $value) = each($options)){
        if ( $value['name'] == 'General Settings' ) {
            $array[] = $value;
            $array[] = array("name" => __('Tracking Code', 'woothemes'),
                "desc" => __('Input tracking code', 'woothemes'),
                "id" => $shortname . "_trackingcode",
                "std" => '',
                "type" => "textarea");
        } else {
            $array[] = $value;
        }
    }
    return $array;
}

参考资料

Customizing WooThemes Options Panel

Adding Theme Options