Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Last active June 19, 2018 19:41
Show Gist options
  • Save tradesouthwest/8b597f49592821d24b1c8578ee037eaa to your computer and use it in GitHub Desktop.
Save tradesouthwest/8b597f49592821d24b1c8578ee037eaa to your computer and use it in GitHub Desktop.
WP dropdown select options field for plugin settings fields
<?php
// d.) settings
add_settings_field(
'woonumday_wndtaxbase_field',
esc_attr__('Tax Options', 'woonumday'),
'woonumday_wndtaxbase_field_cb',
'woonumday_options',
'woonumday_options_section',
array(
'type' => 'select',
'option_group' => 'woonumday_options',
'name' => 'woonumday_wndtaxbase_field',
'value' => esc_attr(
get_option( 'woonumday_options' )['woonumday_wndtaxbase_field'] ),
'options' => array(
"standard" => "Standard",
"reduced" => "Reduced",
"zero" => "Zero" ),
'description' => esc_html__( 'This adjust the Additional Fee tax rate only
- not the product tax rate.', 'woonumday' ),
'tip' => esc_attr__( 'Choices are: standard | reduced | zero
See Woocommerce Settings to set taxes', 'woonumday' )
)
);
// Callback
/**
* d.) 'select' field
* @since 1.0.0
*/
function woonumday_wndtaxbase_field_cb($args)
{
print('<label for="woonumday_wndtaxbase_field">');
if( ! empty ( $args['options'] && is_array( $args['options'] ) ) )
{
$options_markup = '';
$value = $args['value'];
foreach( $args['options'] as $key => $label )
{
$options_markup .= sprintf( '<option value="%s" %s>%s</option>',
$key, selected( $value, $key, false ), $label );
}
printf( '<select name="%1$s[%2$s]" id="%1$s-%2$s">%3$s</select>',
$args['option_group'],
$args['name'],
$options_markup );
}
$tip = $args['tip'];
print('<b class="wntip" title="' . esc_attr($tip) . '"> ? </b></label>');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment