michaeluno / admin-page-framework

Facilitates WordPress plugin and theme development.
http://admin-page-framework.michaeluno.jp/
Other
337 stars 71 forks source link

Nested Fields #248

Closed michaeluno closed 8 years ago

michaeluno commented 8 years ago

There should be an easy way to create nested fields.

michaeluno commented 8 years ago

Now this is included in the 3.8.0/main branch. This lets you repeat a set of fields with different field types without creating a field type or a section.

nested_fields

Usage

Use the content argument of the field definition array and pass nesting field definition arrays.

$this->addSettingFields(
    'my_section_id', // section ID
    array(
        'field_id'      => 'Y',
        'title'         => __( 'Y', 'admin-page-framework-loader' ),
        'description'   => __( 'By passing an array of field definition to the <code>content</code> argument, you can nest fields.', 'admin-page-framework-loader' )
            . ' ' . __( 'Also the <code>type</code> argument can be omitted.', 'admin-page-framework-loader' ),
        'content'       => array(
            array(
                'field_id'      => 'i',
                'title'         => __( 'i', 'admin-page-framework-loader' ),                    
                'type'          => 'textarea',
            ),
            array(
                'field_id'      => 'ii',
                'title'         => __( 'ii', 'admin-page-framework-loader' ),                    
                'type'          => 'color',                    
            ),
            array(
                'field_id'      => 'iii',
                'title'         => __( 'iii', 'admin-page-framework-loader' ),
                'repeatable'    => true,
                'sortable'      => true,
                'content'       => array(
                    array(
                        'field_id'      => 'a',
                        'title'         => __( 'a', 'admin-page-framework-loader' ),                    
                        'type'          => 'image',
                        'attributes'    => array(
                            'preview' => array(
                                'style' => 'max-width: 200px;',
                            ),
                        ),                                
                    ),
                    array(
                        'field_id'      => 'b',
                        'title'         => __( 'b', 'admin-page-framework-loader' ),
                        'content'       => array(
                            array(
                                'field_id'      => 'first',
                                'title'         => __( '1st', 'admin-page-framework-loader' ),                    
                                'type'          => 'color',
                                'repeatable'    => true,
                                'sortable'      => true,
                            ),                                
                            array(
                                'field_id'      => 'second',
                                'title'         => __( '2nd', 'admin-page-framework-loader' ),                    
                                'type'          => 'size',
                            ),
                            array(
                                'field_id'      => 'third',
                                'title'         => __( '3rd', 'admin-page-framework-loader' ),                    
                                'type'          => 'select',
                                'label'         => array(
                                    'x' => 'X',
                                    'y' => 'Y',
                                    'z' => 'Z',                                        
                                ),
                            ),                                    
                        ),
                    ),                            
                    array(
                        'field_id'      => 'c',
                        'title'         => __( 'c', 'admin-page-framework-loader' ),                    
                        'type'          => 'radio',                    
                        'label'         => array(
                            'a' => __( 'Apple', 'admin-page-framework-loader' ),
                            'b' => __( 'Banana', 'admin-page-framework-loader' ),
                            'c' => __( 'Cherry', 'admin-page-framework-loader' ),
                        ),
                        'default'       => 'b',
                    ),                        
                )
            ),                    
        ),
    )
);

Example code can be found here. Please try it and report issues. Thanks.