Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reusable Settings API fields
<?php
/**
* Text Field
*
* A simple text field callback to use with the Settings API. Don't forget to pass in
* the arguments array. Here's an example call to add_settings_field() :
*
* add_settings_field( 'my_option', __( 'My Option' ), 'foo_field_text', 'theme_options', 'general', array(
* 'name' => 'my_theme_options[my_option]',
* 'value' => $options['my_option'], // assuming $options is declared
* 'description' => __( 'Write some description for the field here.' ),
* ) );
*/
function foo_field_text( $args ) {
extract( wp_parse_args( $args, array(
'name' => null,
'value' => null,
'description' => null,
) ) );
?>
<input type="text" name="<?php esc_attr_e( $name ); ?>" value="<?php esc_attr_e( $value ); ?>" />
<?php if ( $description ) : ?><span class="description"><?php echo $description; ?></span><?php endif; ?>
<?php
}
@Mamaduka

This comment has been minimized.

Copy link

@Mamaduka Mamaduka commented Apr 23, 2012

Didn't thought about $args parameter for passing theme options, great idea thanks.

@pippinsplugins

This comment has been minimized.

Copy link

@pippinsplugins pippinsplugins commented Apr 23, 2012

This is good, but I still agree with @zamoose on Twitter that this shouldn't be required. Yes we should be able to define custom callbacks but there should also be default ones included, where all you have to do is define the field type.

@chipbennett

This comment has been minimized.

Copy link

@chipbennett chipbennett commented Apr 24, 2012

I currently use this method to define my own form fields in Oenology, but agree that the form-field markup itself could easily be commoditized into core. The advantage of using this method now is that, if/when it does get rolled into core, it will be fairly trivial to switch to supporting the core method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment