Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created April 23, 2012 12:12
Show Gist options
  • Save kovshenin/2470555 to your computer and use it in GitHub Desktop.
Save kovshenin/2470555 to your computer and use it in GitHub Desktop.
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
Copy link

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

@pippinsplugins
Copy link

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
Copy link

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