1

I have a settings page where I am creating a UI that changes column widths for a layout template. To customize this on my settings page I am using do_settings_fields for output and add_settings_field with a callback function that has multiple fields.

My question is this: Is it wrong to use a single callback for multiple settings fields? Everything is working just fine, and this solution is the easiest way for me to achieve what I need.

  • From a coding/DRY perspective, your approach is just fine (and I use that same approach); however, as-written, this question is primarily opinion-based. – Chip Bennett Nov 18 '13 at 15:28
  • That is great. I saw [this approach][1] while writing my question, but it seems a little overkill to have multiple add_settings_fields. [1]: wordpress.stackexchange.com/questions/116082/… – Nathan Powell Nov 18 '13 at 15:40
  • Here's how I handle it. You may find it helpful. I have only one call to add_settings_section() and add_settings_field(), and only one callback for each data type. – Chip Bennett Nov 18 '13 at 15:56
2

It depends on what the fields do. If they are all of the same type – a label and a text input for example – then, yes, that’s a good approach. You can just use the sixth parameter to create a different output.

But if these fields are different, need different escaping methods (esc_textarea() versus esc_attr()) and different HTML code, then you should use different callbacks.

Let’s say you have stored your data in an array.

Now you have a function or a method in your controller class to create a data object and multiple view objects for the different outputs:

class Settings_Controller
{
    public function __construct()
    {
        $data       = new Settings_Data( 'wpse_123635' ); // option name
        $textarea   = new Textarea( $data, 's1' ); // data plus field id
        $checkbox   = new Checkbox( $data, 's2' );
        $text_field = new Text_field( $data, 's3' );

        add_settings_field(
            's1',
            'A textarea',
            array ( $textarea, 'render' ),
            'my_page',
            'my_section'
        );

        add_settings_field(
            's2',
            'A textarea',
            array ( $checkbox, 'render' ),
            'my_page',
            'my_section'
        );

        add_settings_field(
            's3',
            'A textarea',
            array ( $text_field, 'render' ),
            'my_page',
            'my_section'
        );
    }
}

The views will get their data now from the model $data. They are separately testable, reusable and nicely decoupled. You can use the same view class for multiple fields with other data providers and so on.

When in doubt, keep it simple. But if you realize one function has to do too much, separate its tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.