Theme options not passing data to sanitize callback
-
I have the following code and while the form displays correctly as an options page under ‘appearance’, the values types into the input fields do not get written to database.
A test of the $input array received by the sanitize function reveals it to be empty, yet a check of the $_POST array at the same place shows data to be correctly submitted with the form, just that it doesn’t make it into the $input array… and result is the database isn’t being updated and values not stored.
/* ------------------------------------------------------------------------ * * Setting Registration * ------------------------------------------------------------------------ */ function bm_theme_menu() { add_theme_page( 'Blue Mountains Options', // The title to be displayed in the browser window for this page. 'Blue Mountains Options', // The text to be displayed for this menu item 'administrator', // Which type of users can see this menu item 'bm_theme_display_options', // The unique ID - that is, the slug - for this menu item 'bm_theme_display' // The name of the function to call when rendering the page for this menu ); } // end bm_theme_menu add_action('admin_menu', 'bm_theme_menu'); function bm_theme_display() { ?> <!-- Create a header in the default WordPress 'wrap' container --> <div class="wrap"> <div id="icon-themes" class="icon32"></div> <h2>Blue Mountains Options</h2> <p class="description">There are currently no options. This is just for demo purposes.</p> <!-- Create the form that will be used to render our options --> <form method="post" action="options.php"> <?php settings_fields( 'bm_theme_display_options' ); ?> <?php do_settings_sections( 'bm_theme_display_options' ); ?> <?php submit_button(); ?> </form> </div> <?php } // end bm_theme_display /** * Initializes the theme options page by registering the Sections, * Fields, and Settings. * * This function is registered with the 'admin_init' hook. */ function bm_initialize_theme_options() { if( false == get_option( 'bm_theme_display_options' ) ) { add_option( 'bm_theme_display_options' ); } // end if // First, we register a section. This is necessary since all future options must belong to one. add_settings_section( 'bm_global_info', // ID used to identify this section and with which to register options 'Blue Mountains Global Info', // Title to be displayed on the administration page 'bm_general_options_callback', // Callback used to render the description of the section 'bm_theme_display_options' // Page on which to add this section of options ); // Next, we will introduce the fields for toggling the visibility of content elements. add_settings_field( 'cricos_provider_id', // CRICOS Provider ID 'CRICOS Provider ID', // The label to the left of the option interface element 'bm_cricos_provider_callback', // The name of the function responsible for rendering the option interface 'bm_theme_display_options', // The page on which this option will be displayed 'bm_global_info', // The name of the section to which this field belongs array( // The array of arguments to pass to the callback. In this case, just a description. 'Your CRICOS Provider ID' ) ); add_settings_field( 'abn_no', // CRICOS Provider ID 'ABN Number', // The label to the left of the option interface element 'bm_abn_no_callback', // The name of the function responsible for rendering the option interface 'bm_theme_display_options', // The page on which this option will be displayed 'bm_global_info', // The name of the section to which this field belongs array( // The array of arguments to pass to the callback. In this case, just a description. 'Your ABN Number' ) ); add_settings_field( 'contact_phone', // CRICOS Provider ID 'Contact Phone Number', // The label to the left of the option interface element 'bm_contact_phone_callback', // The name of the function responsible for rendering the option interface 'bm_theme_display_options', // The page on which this option will be displayed 'bm_global_info', // The name of the section to which this field belongs array( // The array of arguments to pass to the callback. In this case, just a description. 'Your Primary Contact Phone Number' ) ); add_settings_field( 'copyright_from', // CRICOS Provider ID 'Copyright starting from (year)', // The label to the left of the option interface element 'bm_copyright_from_callback', // The name of the function responsible for rendering the option interface 'bm_theme_display_options', // The page on which this option will be displayed 'bm_global_info', // The name of the section to which this field belongs array( // The array of arguments to pass to the callback. In this case, just a description. '[yyyy] Year from which to start copyright notice in footer' ) ); // Finally, we register the fields with WordPress register_setting( 'bm_theme_display_options', 'bm_theme_display_options', 'bm_sanitize_display_options' ); /* register_setting( 'bm_theme_display_options', 'cricos_provider_id' ); register_setting( 'bm_theme_display_options', 'abn_no' ); register_setting( 'bm_theme_display_options', 'contact_phone' ); register_setting( 'bm_theme_display_options', 'copyright_from' ); */ } // end sandbox_initialize_theme_options add_action('admin_init', 'bm_initialize_theme_options'); /* ------------------------------------------------------------------------ * * Section Callbacks * ------------------------------------------------------------------------ */ /** * This function provides a simple description for the General Options page. * * It is called from the 'sandbox_initialize_theme_options' function by being passed as a parameter * in the add_settings_section function. */ function bm_general_options_callback() { echo '<p>Setup global information for use on the website</p>'; } // end sandbox_general_options_callback /** * This function renders the interface elements for toggling the visibility of the header element. * * It accepts an array of arguments and expects the first element in the array to be the description * to be displayed next to the checkbox. */ function bm_cricos_provider_callback($args) { $options = get_option('bm_theme_display_options'); // Note the ID and the name attribute of the element should match that of the ID in the call to add_settings_field $html = '<input type="text" id="cricos_provider_id" name="cricos_provider_id" value="' . $options['cricos_provider_id'] . '" />'; // Here, we will take the first argument of the array and add it to a label next to the checkbox $html .= '<label for="cricos_provider_id"> ' . $args[0] . '</label>'; echo $html; } // end sandbox_cricos_provider_callback function bm_abn_no_callback($args) { $options = get_option('bm_theme_display_options'); // Note the ID and the name attribute of the element should match that of the ID in the call to add_settings_field $html = '<input type="text" id="abn_no" name="abn_no" value="' . $options['abn_no'] . '" />'; // Here, we will take the first argument of the array and add it to a label next to the checkbox $html .= '<label for="abn_no"> ' . $args[0] . '</label>'; echo $html; } // end sandbox_abn_no_callback function bm_contact_phone_callback($args) { $options = get_option('bm_theme_display_options'); $phone = ''; if( isset( $options['contact_phone'] ) ) { $phone = $options['contact_phone']; } // end if // Note the ID and the name attribute of the element should match that of the ID in the call to add_settings_field $html = '<input type="text" id="contact_phone" name="contact_phone" value="' . $phone . '" />'; // Here, we will take the first argument of the array and add it to a label next to the checkbox $html .= '<label for="contact_phone"> ' . $args[0] . '</label>'; echo $html; } // end sandbox_contact_phone_callback function bm_copyright_from_callback($args) { $options = get_option('bm_theme_display_options'); $year = ''; if( isset( $options['copyright_from'] ) ) { $year = $options['copyright_from']; } // end if //print_r($options); // Note the ID and the name attribute of the element should match that of the ID in the call to add_settings_field $html = '<input type="text" id="copyright_from" name="copyright_from" value="' . $year . '" />'; // Here, we will take the first argument of the array and add it to a label next to the checkbox $html .= '<label for="copyright_from"> ' . $args[0] . '</label>'; echo $html; } // end sandbox_contact_phone_callback function bm_sanitize_display_options( $input ) { echo "INPUT:<br>\n"; print_r($input); echo "DONE!<br>\n"; // Define the array for the updated options $output = array(); // Loop through each of the options sanitizing the data foreach( $input as $key => $val ) { if( isset ( $input[$key] ) ) { $output[$key] = esc_url_raw( strip_tags( stripslashes( $input[$key] ) ) ); } // end if } // end foreach // Return the new collection return apply_filters( 'sandbox_theme_sanitize_social_options', $output, $input ); } // end sandbox_theme_sanitize_social_options
Thanks in advance… Gut feeling is this is something so obvious I’m just not seeing it!
Cheers
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Theme options not passing data to sanitize callback’ is closed to new replies.