Customizer控件中的多个输入


16

说我有一个自定义控件,但是此控件有2个需要保存的输入,例如:

  • 货币类型和价值
  • 尺寸和计量单位
  • 名字和姓氏
  • 文字和样式
  • 图像和图像尺寸
  • 字体家族和字体粗细

我该怎么办?我看到创建控件时有一个设置选项,但是没有文档说明如何使用它,而在野外完成它的唯一示例是Easy Google Fonts,它没有关于如何完成它的解释,以及很难读。是否可以嵌套控件和节?

到目前为止,我发现的所有教程和文档都讨论了具有单个html输入的控件,尽管API提出了建议,但都没有提及具有多个输入/设置的控件


自定义控件是否与WordPress主题自定义API:codex.wordpress.org/Theme_Customization_API或Settings API:codex.wordpress.org/Settings_API一起使用
雷切尔·贝克

到目前为止,主题mod是我的测试所使用的,尽管我没有偏好,并且会接受其中一个或两个或两者的解释
Tom J Nowell

Answers:


17

这个插件演示了如何做到这一点。值得注意的是,涉及的步骤是:

  • 注册每个设置以更新/更改
  • 创建控件时,将数组作为设置参数
  • 渲染输入时,传递设置键以链接和值
  • 设置键不是设置的名称,而是数组的索引,例如0、1、2
  • 通过访问访问注册到控件的设置 $this->settings

这是代码:

<?php
/*
Plugin Name: TJN Typography Control Demo
Author: Tom J Nowell
Version: 1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

add_action( 'customize_register', 'tjn_customize_register' );
function tjn_customize_register( $wp_customize ) {
    if ( ! isset( $wp_customize ) ) {
        return;
    }
    if ( class_exists( 'WP_Customize_Control' ) ) {

        class Toms_Control_Builder extends WP_Customize_Control {

            public $html = array();

            public function build_field_html( $key, $setting ) {
                $value = '';
                if ( isset( $this->settings[ $key ] ) )
                    $value = $this->settings[ $key ]->value();
                $this->html[] = '<div><input type="text" value="'.$value.'" '.$this->get_link( $key ).' /></div>';
            }

            public function render_content() {
                $output =  '<label>' . $this->label .'</label>';
                echo $output;
                foreach( $this->settings as $key => $value ) {
                    $this->build_field_html( $key, $value );
                }
                echo implode( '', $this->html );
            }

        }

        $section = new TJN_Customizer_Section( $wp_customize, 'test', 'Test', 11 );
        $field = new TJN_Customizer_Field( 'testfield','','Test Control' );
        $field->add_to_section( $wp_customize, $section );
    }
}


class TJN_Customizer_Section {
    public $name='';
    public $pretty_name='';
    public function __construct( WP_Customize_Manager $wp_customize, $name, $pretty_name, $priority=25 ) {
        $this->name = $name;
        $this->pretty_name = $pretty_name;

        $wp_customize->add_section( $this->getName(), array(
            'title'          => $pretty_name,
            'priority'       => $priority,
            'transport'      => 'refresh'
        ) );
    }

    public function getName() {
        return $this->name;
    }
    public function getPrettyName() {
        return $this->pretty_name;
    }
}

class TJN_Customizer_Field {

    private $name;
    private $default;
    private $pretty_name;

    public function __construct( $name, $default, $pretty_name ) {
        $this->name = $name;
        $this->default = $default;
        $this->pretty_name = $pretty_name;
    }

    public function add_to_section( WP_Customize_Manager $wp_customize, TJN_Customizer_Section $section ) {

        $wp_customize->add_setting( $this->name, array(
            'default'        => $this->default,
            'type'           => 'theme_mod',
            'capability'     => 'edit_theme_options'
        ) );
        $wp_customize->add_setting( 'moomins', array(
            'default'        => $this->default,
            'type'           => 'theme_mod',
            'capability'     => 'edit_theme_options'
        ) );
        $wp_customize->add_setting( 'papa', array(
            'default'        => $this->default,
            'type'           => 'theme_mod',
            'capability'     => 'edit_theme_options'
        ) );

        $control = new Toms_Control_Builder(
            $wp_customize, $this->name, array(
            'label'    => $this->pretty_name,
            'section'  => $section->getName(),
            'settings'   => array (
                $this->name,
                'moomins',
                'papa'
            )
        ) );

        $wp_customize->add_control( $control );
    }
}

2
正是我所需要的,现在我可以
安然
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.