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
}
@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