wordpress-settings-api-class icon indicating copy to clipboard operation
wordpress-settings-api-class copied to clipboard

RFC / POC: Repeatable Settings Fields

Open ericfernance opened this issue 9 years ago • 4 comments

Hi Tareq, thanks for writing this I found it really helped speed up creating a WP plugin recently for a client, and a heap easier to work with than the existing WP settings API!

This PR is really not right to merge, it's more a POC or discussion starter about introducing repeatables as field type. If it's of interest I will refine so it can be merged.

Overview

This PR introduces a new field type repeatable created exactly the same was as existing field types. A repeatable is simply a container for other fields of the standard field types that are in the children array of the repeatable. See below:

                    array(
                        'name'    => 'repeatable',
                        'label'   => __( 'A repeatable group', 'wpbstyledcalc' ),
                        'desc'    => __( 'A repetable group', 'wpbstyledcalc' ),
                        'type'    => 'repeatable',
                        'children' => array(
                            array(
                                'name'              => 'big_tolerance',
                                'label'             => __( 'Big Tolerances', 'wpbstyledcalc' ),
                                'desc'              => __( 'What value should we use for the larger tolerance', 'wpbstyledcalc' ),
                                'placeholder'       => __( 'Big Tolerance...', 'wpbstyledcalc' ),
                                'type'              => 'text',
                                'default'           => '25',
                            ),
                            array(
                                'name'              => 'small_tolerance',
                                'label'             => __( 'Small Tolerances', 'wpbstyledcalc' ),
                                'desc'              => __( 'What value should we use for the small tolerance', 'wpbstyledcalc' ),
                                'placeholder'       => __( 'Small Tolerance...', 'wpbstyledcalc' ),
                                'type'              => 'text',
                                'default'           => '25',
                            ),
                        )
                    )

This would show up in the settings page as:

2016-10-19_07-10-56

On the form submit the data is json encoded into a hidden input field that matches the top level name of the repeatable type. In this case repeatable.

On the form display the JSON encoded option is parsed and the data used to populate the children element array.

The Add button will add a new row and the X button will delete an existing row.

Work to Be Done

If there's interest in adding this feature the main work that still needs to be done is to move fields other than text into the repeatable when creating the child fields for the markup.

Currently the only field that works as a repeatable child is a text field

To do this properly I would probably slightly modify the existing callback_type methods to allow their use in the repeatable as well as stand alone fields.

I would also like to remove some of the inline javascript and move into the jquery.repeatable.js file.

ericfernance avatar Oct 18 '16 21:10 ericfernance

Hello @digitalgarage, awesome work! The main goal of this class is to remain as simple as possible to provide settings solution. That's why I like to keep this into just a single file, no dependencies, just copy a single file and do the work.

While the idea of a repeatable field is great and mostly used in the custom field libraries, there might be some use of it for sure, but I am not sure bringing the repeatable field in the settings area is a good idea and keep the class in a single file as well.

That's what I feel, not sure what others think though.

tareq1988 avatar Oct 24 '16 03:10 tareq1988

Hey @tareq1988 for sure I hear what you are saying!

I coded it mainly for a specific use case I have so I'll keep working on it anyway, since I need to make repeatables out of checkboxes and selects as well (and inline the JS). If there's enough interest then feel free to merge it in, if not that's cool too.

Eric.

ericfernance avatar Oct 25 '16 00:10 ericfernance

Great work @digitalgarage, I can see myself using this feature if it's ever implemented.

factmaven avatar Oct 25 '16 20:10 factmaven

Repeatable fields are very useful in settings page as well. I'm developing a plugin where I need it. @digitalgarage did you manage to implement other field types in repeatable? If yes, can you please share the code?

manzoorwanijk avatar May 22 '17 15:05 manzoorwanijk