1
votes

I just created a simple theme option page that is working fine and also saved after when press save options.

But when I go somewhere else from theme options page and come back to theme options page that settings what I saved just disappear and I have to change that again whenever I come to theme options page.

Here is my code

add_action( 'admin_menu', 'theme_options_add_page' );

if ( get_option('new_theme_options')) {
$theme_options = get_option('new_theme_options');
} else {
add_option('new_theme_options', array (
    'sidebar2_on' => true,
    'footer_text' => ''
));
$theme_options = get_option('new_theme_options');

}

function theme_options_add_page() {
add_submenu_page( 'themes.php', 'My Theme Options', 'Theme Options', 8,    'themeoptions', 'theme_options_do_page' );
}

function theme_options_do_page() {
global  $theme_options;



    $new_values = array (
        'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
        );
update_option('new_theme_options', $new_values);

$theme_options = $new_values;

?>
    <div class="wrap">
    <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme   Options', 'responsivetheme' ) . "</h2>"; ?>




 <form method="post" action="themes.php?page=themeoptions">

 <label for="footer_text">Footer Text:</label>
 <input id="footer_text" type="text" name="footer_text" value="<?php echo   $theme_options['footer_text']; ?>" />

    <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
        </p>
    </form>
</div>
 <?php
}
2
Where are you using this code? What happens if you disable all plugins? Did you test using the code in a clean theme? Any messages from WP_DEBUG? - brasofilo
yes my theme is clean and I am using it in funcions.php and I dont have much plugins and I am also using it on localhost. I did not enable WP_DEBUG. - user2579060
Notice: Undefined variable: defaults in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 88 Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 359 Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 361 this is the msgs from WP_DEBUG - user2579060
Ok, than you have to handle this Notices accordingly, using isset() mainly and providing alternatives when the var does not exist yet. Can you test my code on another theme that you haven't modified? It works for me and your problem seems to be elsewhere, outside the sample code you provided. - brasofilo

2 Answers

1
votes

@Praveen answer is correct, but for completeness I'll post the full code I tested. Please, note that you should always develop with WP_DEBUG enabled. It shows three issues with your code:

  • using $_POST['footer_text'] without it being defined (Praveen's answer)
  • using the deprecated function get_current_theme()
  • using Level instead of Capability in add_submenu_page()

I dropped the following code into my theme's functions.php and it works ok:

if ( get_option('new_theme_options')) {
    $theme_options = get_option('new_theme_options');
} else {
    $theme_options = array (
        'sidebar2_on' => true,
        'footer_text' => ''
    );
    add_option( 'new_theme_options', $theme_options );
}

add_action( 'admin_menu', 'theme_options_add_page' );

function theme_options_add_page() {
    add_submenu_page( 
        'themes.php', 
        'My Theme Options', 
        'Theme Options', 
        'add_users',    
        'themeoptions', 
        'theme_options_do_page' 
    );
}

function theme_options_do_page() {
    global  $theme_options;
    if( isset( $_POST['footer_text'] ) ) {
        $new_values = array (
            'footer_text' => htmlentities( $_POST['footer_text'], ENT_QUOTES),
        );
        update_option('new_theme_options', $new_values);
        $theme_options = $new_values;
    } 
    ?>
    <div class="wrap">
        <?php screen_icon(); echo "<h2>" . wp_get_theme() . __( ' Theme   Options', 'responsivetheme' ) . "</h2>"; ?>
        <form method="post" action="themes.php?page=themeoptions">
            <label for="footer_text">Footer Text:</label>
            <input id="footer_text" type="text" name="footer_text" value="<?php echo   $theme_options['footer_text']; ?>" />
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
            </p>
        </form>
    </div>
    <?php
}

The only issue I can see is the option value sidebar2_on that's being overwritten in theme_options_do_page(), but your sample code does not show it being used elsewhere.

1
votes

Please change this code:

if($_POST['footer_text']) {
$new_values = array (
    'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
    );
update_option('new_theme_options', $new_values);
$theme_options = $new_values;
}

hope this will work fine :)