• Hello everyone. I am writing a WordPress plugin that makes use of the Settings API to register a few options. When I try to save my options, they do not save. The fields are blank when the page reloads and there is no message that the options saved. I’ve been racking my brain for hours and cannot find any answers. I would be very grateful for another set of eyes to take a look and point out what I’m missing. Here is the (slightly modified) start to my code for the options page:

    /**************************************** Register Settings Menu & Page ****************************************/
    
    add_action( 'admin_menu', 'rv_admin_menu' );
    function rv_admin_menu() {
    
    	/** // Menu Title, Page Title, Capability, Slug, Callback Function
    	add_theme_page( 'Misc.  Options', 'Misc.  Options', 'manage_options', 'rv-options-page', 'rv_options_create_page' ); **/
    
    	// Parent Slug, Page Title, Menu Title, Capability, Menu Slug, Callback Function
    	add_submenu_page('', 'Misc.  Options', 'Misc.  Options', 'manage_options', 'rv-options-page', 'rv_options_create_page');
    }
    
    /**************************************** Register Settings ****************************************/
    
    add_action( 'admin_init', 'rv_admin_init' );
    function rv_admin_init() {
    
    	/** Add the Settings Group **/
    	register_setting( 'rv-options-group', 'rv-settings' ); // Option Group & Options Name
    
    	/* Add Custom Footer Section & Fields */
    	// ID, Title, Callback, Menu Page
    	add_settings_section( 'footer-section', 'Custom Footer', 'custom_footer_section_callback', 'rv-options-page' );
    	// ID, Title, Callback, Menu Page, Associated Section
    	add_settings_field( 'custom-footer-enable', 'Enable Custom Footer', 'custom_footer_enable_callback', 'rv-options-page', 'footer-section' );
    	add_settings_field( 'footer-content', 'Footer Content', 'custom_footer_callback', 'rv-options-page', 'footer-section' ); 
    
    	/* Add Author Box Section & Fields */
    	add_settings_section( 'author-box', 'Author Box', 'author_box_section_callback', 'rv-options-page' );
    	add_settings_field( 'author-box-enable', 'Globally Enable Author Box', 'author_box_enable_callback', 'rv-options-page', 'author-box' );
    	add_settings_field( 'author-box-title', 'Author Box Title', 'author_box_title_callback', 'rv-options-page', 'author-box' );
    
    }
    
    /**************************************** Footer ****************************************/
    
    /** Custom Footer Section Intro **/
    function custom_footer_section_callback() {
    	echo "Add your footer's custom HTML.";
    }
    
    /* Custom Footer Enable */
    function custom_footer_enable_callback() {
    	$setting_footer_enable = get_option( 'rv-settings' );
    	$custom_footer_enable = esc_attr( $setting_footer_enable['footer_enable'] );
    	echo "<input type='checkbox' name='rv-settings[footer_enable]' value='$custom_footer_enable' />";
    	echo " The settings below will not work unless this option is enabled.";
    }
    
    /* Custom Footer Text Area */
    function custom_footer_callback() {
    	$setting_footer = get_option( 'rv-settings' );
    	$footer = esc_attr( $setting_footer['footer'] );
    	echo "<textarea rows='12' cols='100' name='rv-settings[footer]' value='$footer'></textarea>";
    }
    
    /**************************************** Author Box ****************************************/
    
    /** Author Box Section Intro **/
    function author_box_section_callback() {
    	echo "Customize the Author Box";
    }
    
    /* Globally Enable Author Box on Posts */
    function author_box_enable_callback() {
    	$setting_author_box_enable = get_option( 'rv-settings' );
    	$enable_author_box = esc_attr( $setting_author_box_enable['enable_author_box'] );
    	echo "<input type='checkbox' name='rv-settings[enable_author_box]' value='$enable_author_box' />";
    	echo " This will globally enable the author box on Posts.";
    }
    
    /* Custom Author Box Title */
    function author_box_title_callback() {
    	$setting_author_box_title = get_option( 'rv-settings' );
    	$author_box_title = esc_attr( $setting_author_box_title['author_box_title'] );
    	echo "<input size='100' type='text' name='rv-settings[author_box_title]' value='$author_box_title' />";
    	echo "<br/> This will modify the author box title. <em>Ex: '<h2>Meet the Author</h2>'";
    }
    
    /**************************************** Create Settings Page ****************************************/
    
    function rv_options_create_page() { ?>
        <div class="wrap">
            <h2>RV  Options</h2>
            <form action="options.php" method="POST">
                <?php settings_fields( 'rv-options-group' ); // Options Group ?>
                <?php do_settings_sections( 'rv-options-page' ); // Menu Page ?>
                <?php submit_button(); ?>
            </form>
        </div>
        <?php
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Your data is being saved to the database just fine, but you’re outputting it incorrectly for the checkboxes and the textarea.

    For the checkboxes, the value should be just a boolean value, like “1”, and you should use the checked() function to output the checked state to the input field. Like so:

    $custom_footer_enable = $setting_footer_enable['footer_enable'];
    echo "<input type='checkbox' name='rv-settings[footer_enable]' value='1' ";
    checked($custom_footer_enable, true);
    echo " />";

    Same goes for the other checkbox.

    For a textarea, it doesn’t have a value attribute, it simply contains the information inside the textarea html, and you should also escape it properly. Like so:

    $footer = esc_textarea( $setting_footer['footer'] );
    echo "<textarea rows='12' cols='100' name='rv-settings[footer]'>$footer</textarea>";
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Oh, additionally, you should add a validation function callback to the register_setting call, because the thing about checkboxes is that they only return the “on” state, not the “off” state. So you will never get data back of rv-settings[footer_enable] = 0.

    Thus, in your validation function, you need to check if the rv-settings[footer_enable] is empty(), and then manually set that array value to zero there, so that the turning-it-off works properly. The validation function takes an array of values as input, and returns the array of validated/sanitized values as output. The output will be what is saved to the database.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Settings API Code Not Saving Options’ is closed to new replies.