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.
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()
versusesc_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:
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.