I am trying to develop a theme. I am at the part of using the settings API. The code I use for the settings are as follows:
In the example snippets I'm using variables to show the structure of my code. Ofcourse in my actual code the variables are strings (the callback variables are no strings)
register_setting($option_group, $options_name);
add_settings_section($id, $title, $callback_section, $page);
add_settings_field($id, $title, $callback_field, $page, $section);
function $callback_section()
{
}
function $callback_field()
{
echo '<input type="checkbox" name="$option_name" />';
}
for the form in my theme options page:
<form method="post" action="options.php">
<?php
settings_fields($option_group);
do_settings_sections($page);
submit_button();
settings_errors();
?>
</form>
My settings are saved but the input returns to the unchecked state. How can I make it work? Did I miss anything? Also.. I will need alot of settings. What is the best approach for handling alot of settings with the settings API without getting messy code?
I am trying to develop a theme. I am at the part of using the settings API. The code I use for the settings are as follows:
In the example snippets I'm using variables to show the structure of my code. Ofcourse in my actual code the variables are strings (the callback variables are no strings)
register_setting($option_group, $options_name);
add_settings_section($id, $title, $callback_section, $page);
add_settings_field($id, $title, $callback_field, $page, $section);
function $callback_section()
{
}
function $callback_field()
{
echo '<input type="checkbox" name="$option_name" />';
}
for the form in my theme options page:
<form method="post" action="options.php">
<?php
settings_fields($option_group);
do_settings_sections($page);
submit_button();
settings_errors();
?>
</form>
My settings are saved but the input returns to the unchecked state. How can I make it work? Did I miss anything? Also.. I will need alot of settings. What is the best approach for handling alot of settings with the settings API without getting messy code?
Share Improve this question edited Jun 13, 2020 at 11:58 Leo asked Jun 13, 2020 at 7:09 LeoLeo 33 bronze badges1 Answer
Reset to default 0$options = get_option( 'textdomain_settings' );
?>
<input type='checkbox' name='textdomain_settings[textdomain_checkbox_field_0]' <?php checked( $options['textdomain_checkbox_field_0'], 1 ); ?> value='1'>
<?php
Use this settings under Callback Field Function or you can generate code form here for settings api directly.