WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 2 months ago

#21488 reopened enhancement

Add Default Callback Functions for add_settings_field()

Reported by: mordauk Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch dev-feedback settings-api
Focuses: administration Cc:

Description (last modified by scribu)

By default, when creating options in plugins and themes, every developer is required to create custom callback functions for rendering their option's HTML. The HTML for most options is nothing more than a standard INPUT field, a SELECT field, TEXTAREA field, etc, so there's really no reason there shouldn't be default callback options in place.

For example, if I have a plugin that registers one text field option in the General settings page, it really doesn't make sense that I should be forced to create a callback function, especially not when probably 99% of all text fields are outputted in exactly the same way:

<input name="FIELD NAME" id="FIELD ID" value="FIELD VALUE" class="regular-text"/>
<div class="description">The description of the field (if present)</div>

With default field callbacks available, developers can do this:

function pw_register_settings() {
	register_setting( 'general', 'pw_sample_option', 'esc_attr' );
	add_settings_section( 'pw_sample_section', 'This is a Sample Section', 'pw_sample_section_cb', 'general');
	add_settings_field( 'pw_sample_option', 'A Sample Setting', 'text', 'general', 'pw_sample_section', array( 'description' => 'The field description' ) );
}
add_action('admin_init', 'pw_test_settings');

function pw_sample_section_cb() {
	// this is the section HTML (if you want it)
}

This is much simpler than also having to write the callback function to render the HTML for the option.

The patch attached adds the following default callbacks:

  • text
  • textarea
  • select
  • radio
  • checkbox
  • checkbox_group

For select, radio, and checkbox groups, the options are passed as an array of "choices" in the last, optional $args parameter for add_settings_field():

$options = array( 
	'one' => 'The Choice Name',
	'two' => 'The Second name', 
	'three' => 'The Third option'
);
add_settings_field( 'pw_sample_option', 'A Sample Setting', 'select', 'general', 'pw_sample_section', array( 'choices' => $options, 'description' => 'This is a select' ) );

When a user wants to create a custom callback function, this is still allowed as call_user_func() is the default in the $field['callback'] switch statement for the do_settings_fields() function.

Attachments (2)

default_callbacks.diff (5.4 KB) - added by mordauk 21 months ago.
Adds default callbacks for add_settings_field()
add_args_to_settings_section_callback.patch (1.6 KB) - added by cfoellmann 8 months ago.
hand over args to add_settings_section callback function

Download all attachments as: .zip

Change History (46)

mordauk21 months ago

Adds default callbacks for add_settings_field()

comment:1 themeblvd21 months ago

  • Cc themeblvd added

comment:2 scribu21 months ago

Overloading the $callback parameter seems like a slick solution. +1

comment:3 scribu21 months ago

  • Description modified (diff)

comment:4 sabreuse21 months ago

  • Cc sabreuse@… added

comment:5 Japh21 months ago

  • Cc japh@… added

comment:6 jbobich21 months ago

This would really help make it easier to create basic option pages and add more consistency among plugin and theme authors with their option panels. Make it so. +1

Some issues mordauk and I had when putting this together today was getting "default" option values to work with checkbox and checkbox groups, so we left it off those option types.

Also, if one does choose to use these default option type callbacks the way we've got them here, you must have each option setup as a standard, single option field. In other words, you couldn't have your settings page work by saving all your settings to a single option field as a multi-dimensional array. -- Not sure if this is too serious of an issue, but maybe someone could have a look at that part of things and see if it's possible to allow that somehow.

Last edited 21 months ago by jbobich (previous) (diff)

comment:7 follow-ups: scribu21 months ago

How about, instead of overloading the callback arg, you just skip it entirely and use a 'type' option instead? For example:

$args = array(
	'type' => 'select',
	'choices' => array(
		'one' => 'The Choice Name',
		'two' => 'The Second name', 
		'three' => 'The Third option'
	),
	'description' => 'This is a dropdown.'
);

add_settings_field( 'pw_sample_option', 'A Sample Setting', false, 'general', 'pw_sample_section', $args );

Starts to look a lot like Customizer fields, don' it?

Last edited 21 months ago by scribu (previous) (diff)

comment:8 in reply to: ↑ 7 sc0ttkclark21 months ago

We actually use this same format for Pods 2.0 field handling, I recommend it. Maybe instead of choices, 'data', but that's just a suggestion. I'd also like to see a custom callback available for the type, or as long as the type can be anything and runs through an action through an 'else', then that's great.

Replying to scribu:

How about, instead of overloading the callback arg, you just skip it entirely and use a 'type' option instead?

comment:9 sc0ttkclark21 months ago

  • Cc lol@… added

comment:10 follow-up: boonebgorges21 months ago

  • Cc boonebgorges@… added

A big +1.

In fact, I suggest that these be moved out of wp-admin and into wp-includes, so that front-end plugin and theme developers could use the same methods for building form markup. There's nothing settings-specific about them as they're currently written.

comment:11 in reply to: ↑ 7 mordauk21 months ago

  • Resolution set to fixed
  • Status changed from new to closed

Replying to scribu:

How about, instead of overloading the callback arg, you just skip it entirely and use a 'type' option instead? For example:

$args = array(
	'type' => 'select',
	'choices' => array(
		'one' => 'The Choice Name',
		'two' => 'The Second name', 
		'three' => 'The Third option'
	),
	'description' => 'This is a dropdown.'
);

add_settings_field( 'pw_sample_option', 'A Sample Setting', false, 'general', 'pw_sample_section', $args );

Starts to look a lot like Customizer fields, don' it?

jbobich and I thought about that for a while and ultimately thought it made more sense to overload the callback instead of having a type option in the $args parameter. The default field types we've proposed here are still callbacks in terms of what they do, so I don't think they should be separated. I have a feeling it would get confusing for users if they saw that they could define a callback AND a type but only one could actually be used. There would be some users who would create a text field callback and then also set the type to "text" not realizing that only one or the other was necessary.

comment:12 mordauk21 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:13 in reply to: ↑ 10 ; follow-up: sc0ttkclark21 months ago

Replying to boonebgorges:

A big +1.

In fact, I suggest that these be moved out of wp-admin and into wp-includes, so that front-end plugin and theme developers could use the same methods for building form markup. There's nothing settings-specific about them as they're currently written.

Oh this is gold.

comment:14 in reply to: ↑ 13 mordauk21 months ago

Replying to sc0ttkclark:

Replying to boonebgorges:

A big +1.

In fact, I suggest that these be moved out of wp-admin and into wp-includes, so that front-end plugin and theme developers could use the same methods for building form markup. There's nothing settings-specific about them as they're currently written.

Oh this is gold.

Completely agree. Perhaps instead of do_settings_text(), the functions should just be text_field(), select_field(), etc. This would match the pattern already used for the submit_button() function.

comment:15 ocean9021 months ago

  • Cc ocean90 added

comment:16 toscho21 months ago

  • Cc info@… added

comment:17 WraithKenny20 months ago

I've done something similar (with the $args overloading) but without the switch ($field['callback']) stuff:
https://github.com/unFocus/Scripts-n-Styles/blob/master/includes/class-sns-settings-page.php#L57

It references the methods here (it's only a class for naming collision prevention):
https://github.com/unFocus/Scripts-n-Styles/blob/master/includes/class-sns-form.php

The difference is I had to dual purpose label_for which *should* match the $field['id']

+1 for any sort of input html library in core tho :)

comment:18 rzen20 months ago

+1000 to this. I want to see this happen very much. I built the Options API for http://wpstartbox.com because I wanted to have something handy for all my callbacks (of course, I made it do a bunch of other things too).

I'll give this a test and report back.

comment:19 cais20 months ago

  • Cc edward.caissie@… added

comment:20 husobj20 months ago

  • Cc ben@… added

comment:21 stephenh198820 months ago

  • Cc contact@… added

comment:22 nvwd20 months ago

  • Cc nowell@… added

comment:23 follow-up: nacin20 months ago

Rather than overloading add_settings_field()'s $callback argument with a type-like name (as scribu points out), why not just name the actual function there?

add_settings_field( 'upload_path', __( 'Upload path' ), 'do_settings_text',
    'media', 'uploads',
    array(
         'id' => 'upload_path', // defaults to first argument,
         'name' => 'upload_path', // defaults to first argument, then ['id']
         'class' => 'regular-text code',
         'description' => __( 'Default is <code>wp-content/uploads</code>' ),
);

comment:24 in reply to: ↑ 23 mordauk20 months ago

Replying to nacin:

Rather than overloading add_settings_field()'s $callback argument with a type-like name (as scribu points out), why not just name the actual function there?

add_settings_field( 'upload_path', __( 'Upload path' ), 'do_settings_text',
    'media', 'uploads',
    array(
         'id' => 'upload_path', // defaults to first argument,
         'name' => 'upload_path', // defaults to first argument, then ['id']
         'class' => 'regular-text code',
         'description' => __( 'Default is <code>wp-content/uploads</code>' ),
);

I prefer overloading it instead because that allows the user to just pass "text" or "textarea" as the callback, which makes it simpler. If the callback isn't overloaded and just the function name is passed, then I think the default callback functions should be named something more intuitive, perhaps just "text_field", "textarea_field", etc.

comment:25 bpetty20 months ago

  • Keywords dev-feedback added

This appears to be one implementation (a child) of #18285, which I know a couple devs discussing this patch are aware of, but it hasn't been mentioned in this ticket, so I'm pointing it out now. Take a look there for other ideas on how a new settings API could be
implemented.

Also related: #9296 (in reference to lack of flexible API for custom permalink settings)

comment:26 bpetty20 months ago

  • Cc bpetty added
  • Milestone changed from Awaiting Review to Future Release

comment:27 Otto4217 months ago

  • Cc Otto42 added

comment:28 sunnyratilal17 months ago

  • Cc ratilal.sunny@… added

comment:29 cklosows17 months ago

  • Cc cklosowski@… added

comment:30 dougal17 months ago

This isn't going to make it into 3.5?

Darnit, I was all set to update my plugins.

comment:31 dougal17 months ago

  • Cc dougal@… added

comment:32 follow-up: kovshenin17 months ago

  • Cc kovshenin added

If this does go into core, I think it should be part of #18285 and not global functions.

comment:33 in reply to: ↑ 32 mordauk17 months ago

Replying to kovshenin:

If this does go into core, I think it should be part of #18285 and not global functions.

I would actually like to see it done with both. As it stands, this is a patch that can be implemented at anytime. The entire new Settings API class, however, is still a long ways out.

While it might be a temporary improvement, it would still be hugely helpful for the next year, year and a half while the new class is worked on.

comment:34 unknowndomain16 months ago

  • Keywords settings-3.6 added

comment:35 unknowndomain16 months ago

  • Cc me@… added

comment:36 SergeyBiryukov16 months ago

  • Keywords settings-api added; settings-3.6 removed

comment:37 dcowgill15 months ago

  • Cc dcowgill@… added

comment:38 zamoose14 months ago

  • Cc zamoose@… added

comment:39 cfoellmann8 months ago

  • Cc foellmann@… added

cfoellmann8 months ago

hand over args to add_settings_section callback function

comment:40 cfoellmann8 months ago

Added an $args array to the function

add_settings_section( $id, $title, $callback, $page )

which results in

add_settings_section( $id, $title, $callback, $page, $args = array() )

Example functions:

function register_some_settings() {
	add_settings_section( 
		'some_id', // $id
		'Some Title', // $title
		'generate_section', // $callback
		'some_page', // $page
		array(
			"desc" => "some text for the section description"
		)
	);
	
	// add some fields ...
	
	add_settings_section( 
		'some_other_id', // $id
		'Another Title', // $title
		'generate_section', // $callback
		'some_page', // $page
		array(
			"desc" => "A DIFFERENT text for the second section's description"
		)
	);
}

function generate_section( $args ) {
	echo '<p>' . $args['desc'] . '</p>';
}

comment:41 cfoellmann8 months ago

moved the patch to a dedicated ticket since it is not really in the scope of the ticket.
See #25154

comment:42 nacin3 months ago

  • Component changed from General to Plugins

comment:43 nacin2 months ago

  • Component changed from Plugins to Admin APIs
  • Focuses administration added

comment:44 nacin2 months ago

  • Component changed from Admin APIs to Plugins

Sorry for the noise.

Note: See TracTickets for help on using tickets.