May 28, 201313 yr Hi, I'm having my first go at creating a Wordpress Theme Options page and have been following a couple of tutorials but not successfully able to achieve what I want. The page is appearing and formatting well enough but it's just not saving the options or acknowledging the options are saved. Here's my code so far (largely borrowed from http://www.onedesigns.com/tutorials/how-to-create-a-wordpress-theme-options-page): <?php $sa_options = array( 'primary_colour' => '#4385B2', 'secondary_colour' => '#d93600', 'header_image' => '', 'header_colour' => '#EFEFEF', 'logo_image' => '', 'social_links' => '' ); if ( is_admin() ) : add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' ); function mw_enqueue_color_picker( $hook_suffix ) { // first check that $hook_suffix is appropriate for your admin page wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'my-script-handle', content_url('themes/towntown/options/js/admin.js', __FILE__ ), array( 'wp-color-picker' ), false, true ); } function sa_register_settings() { register_setting( 'sa_theme_options', 'sa_options', 'sa_validate_options' ); } add_action( 'admin_init', 'sa_register_settings' ); $settings = get_option( 'sa_options', $sa_options ); function sa_theme_options() { add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'sa_theme_options_page' ); } add_action( 'admin_menu', 'sa_theme_options' ); function sa_theme_options_page() { global $sa_options; if ( ! isset( $_REQUEST['settings-updated'] ) ) $_REQUEST['settings-updated'] = false; // This checks whether the form has just been submitted. ?> <div class="wrap"> <?php screen_icon(); echo "<h2>" . wp_get_theme() . ' Theme Options' . "</h2>"; // This shows the page's name and an icon if one has been provided ?> <?php if ( false !== $_REQUEST['settings-updated'] ) : ?> <div><p><strong><?php 'Options saved' ; ?></strong></p></div> <?php endif; // If the form has just been submitted, this shows the notification ?> <form method="post" action="options.php"> <?php $settings = get_option( 'sa_options', $sa_options ); ?> <?php settings_fields( 'sa_theme_options' ); /* This function outputs some hidden fields required by the form, including a nonce, a unique number used to ensure the form has been submitted from the admin page and not somewhere else, very important for security */ ?> <table><!-- Grab a hot cup of coffee, yes we're using tables! --> <tr valign="top"><th scope="row"><label for="primary_colour">Primary Colour:</label></th> <td> <input id="primary_colour" name="sa_options[primary_colour]" type="text" value="<?php echo $settings['primary_colour']; ?>" class="colour-field" /> </td> </tr> <tr valign="top"><th scope="row"><label for="secondary_colour">Secondary Colour: </label></th> <td> <input id="secondary_colour" name="sa_options[secondary_colour]" type="text" value="<?php echo $settings['secondary_colour']; ?>" class="colour-field" /> </td> </tr> <tr valign="top"><th scope="row"><label for="header_image">Header Image: </label></th> <td> <input id="header_image" name="sa_options[header_image]" type="text" value="<?php echo $settings['header_image']; ?>" /> </td> </tr> <tr valign="top"><th scope="row"><label for="header_colour">Header Background Colour: </label></th> <td> <input id="header_colour" name="sa_options[header_colour]" type="text" value="<?php echo $settings['header_colour']; ?>" class="colour-field" /> </td> </tr> <tr valign="top"><th scope="row"><label for="logo_image">Logo: </label></th> <td> <input id="logo_image" name="sa_options[logo_image]" type="text" value="<?php echo $settings['logo_image']; ?>" /> </td> </tr> <tr valign="top"><th scope="row"><label for="social_links">Social Links: </label></th> <td> <input id="social_links" name="sa_options[social_links]" type="text" value="<?php echo $settings['social_links']; ?>" /> </td> </tr> </table> <p><input type="submit" value="Save Options" /></p> </form> </div> <?php function sa_validate_options( $input ) { global $sa_options; $settings = get_option( 'sa_options', $sa_options ); // We strip all tags from the text field, to avoid vulnerablilties like XSS $input['primary_colour'] = wp_filter_nohtml_kses( $input['primary_colour'] ); $input['secondary_colour'] = wp_filter_post_kses( $input['secondary_colour'] ); $input['header_image'] = wp_filter_nohtml_kses( $input['header_image'] ); $input['header_colour'] = wp_filter_post_kses( $input['header_colour'] ); $input['logo_image'] = wp_filter_nohtml_kses( $input['logo_image'] ); $input['social_links'] = wp_filter_post_kses( $input['social_links'] ); return $input; } } endif; // EndIf is_admin() Appreciate any pointers, been at this for a while now and it's getting very annoying... :/ Many thanks, ~evu.
June 3, 201313 yr Author Hi Caleb, I'm not 100% on this side of things but I think with the changes to how we create theme options, Wordpress have included a function that does that for you. register_setting( 'sa_theme_options', 'sa_options', 'sa_validate_options' ); Is the part that saves the options, I think. Let me know if I'm wrong though, I'm semi-blindly following tutorials trying to get this to work. Cheers.
June 8, 201313 yr It seems there is no functions to save the options. Maybe you haven't copied all the codes. And this function "update_option( 'sa_options', $sa_options)" is the one you lost, which has the function of saving options. You should find the code segment include this.
Create an account or sign in to comment