WordPress My theme options settings not saving

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.

Read More

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
}

Related posts

Leave a Reply

2 comments

  1. @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.

  2. 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 🙂