I am trying to create a simple checkbox setting that toggles a section in a blog post on or off in a theme settings page.
Unfortunately it's not saving the checkbox setting. I check the box and when I refresh the page it is unchecked again. Either it is not saving the setting or the checked function isn't working. Am I missing something?
function theme_option_settings(){
register_setting( 'prev-next-setting', 'prev-next' );
add_settings_section( 'blog-section', 'Blog Section', 'change_blog_layout_section', 'theme-options' );
add_settings_field( 'show-prev-next', 'Show Previous/Next Post Section', 'show_prev_next_field', 'theme-options', 'blog-section' );
}
function change_blog_layout_section(){
echo "Change the blog layout section";
}
function show_prev_next_field(){
echo get_option( 'prev-next' );
echo "<input type='checkbox' id='prev-next' name='prev-next' value='1' ".checked(1, get_option('prev-next'), true)."/>";
}
checked(),$echo, should befalse, because you're already using it as part of an echo statement. If you just need to include it in a string, you need to return it.'1', a String, but you're using1, an integer, in thechecked()function. The types need to match. Use'1'(in quotes) in the checked function.