I have a snippet that creates an option in Settings->General using Settings API. However, the values don’t get updated and I was wondering if anyone can help point out the problem with the following code
function admin_init_callback() {
register_setting( 'msp_settings', 'msp_duration' );
add_settings_section( 'msp_settings', __('Multisite Posts Configuration'), false, 'general' );
add_settings_field( 'msp_duration', __('Cache Duration'), array($this, 'msp_settings_callback'), 'general', 'msp_settings', array(
"id" => "msp_duration",
"type" => "dropdown",
"values"=> array(
array(
"name" => __('Every 6 hours'),
"value" => 60 * 60 * 6,
),
array(
"name" => __('Every 12 hours'),
"value" => 60 * 60 * 12,
),
array(
"name" => __('Every 24 hours'),
"value" => 60 * 60 * 24,
),
array(
"name" => __('Every 48 hours'),
"value" => 60 * 60 * 48,
),
array(
"name" => __('Every Week'),
"value" => 60 * 60 * 24 * 7,
),
)
) );
return;
}
function msp_settings_callback($args) {
$value = get_option( $args["id"] );
if("dropdown" == $args["type"]) {
var_dump($value);
?>
<select name="<?php echo $args["id"]; ?>">
<?php
foreach ($args["values"] as $option) {
echo '<option value="' . $option["value"] . '" ';
checked( $value, $option["value"], $echo = true );
echo '>' . $option["name"] . '</option>';
}
?>
</select>
<?php
}
}
Here you go my friend:
Use the value with:
get_option( 'msp_duration' );
Instead of checked, you should use selected function
So your msp_settings_callback will look like this