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

But whene reload page by pressing "theme options" menu the saved settings disappeared.

Or if you go another page and come back to theme options page the same thing happens.

but just reloading page by refresh(F5) the page settings dont affected.

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
}

Please help.

Hi,

I just check the code. Can you change your add option and check?
Insted of array use individual saving options.

add_option( 'sidebar2_on', 'true', '', 'yes' ); 
add_option( 'footer_text', '(c) mytheme', '', 'yes' );

Please check and let me know.
Thanks,
Ajay

no its not working,
I hope you understand my problem.

Hi,

Ok. Let me check tonight when I come home. Meanwhile you can see the codex docx for your issue.
Click Here
Click Here

Thanks,
Ajay

I already see this codex.

Hi,

Finally I have trace out issues and updte the code.
a) Save Theme option as single (Not an array)
b) Add name to "Save Option" button
c) For Saving First check that the request is came from form. If it came from form then update it. Other wise skip it. To do this use isset() function.
d) comment some unnecessary function and code. Please find updated code.
I test this code as plugin, So plece this file in "wp-content\plugin\ajaytest\" folder.
Go to Plugn and activate it. You can see the menu in Admin section. I checked this on "Twenty Ten" WP Theme. Please see the coplete code.

<?php
/*
Plugin Name: Ajay Test Plugin
Plugin URI: http://www.ajaygokhale.net
Description: Test Plugin
Version: 1.0
Author: Ajay Gokhale
Author URI: http://www.ajaygokhale.net/
*/
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);
    if( isset($_POST['submit']) ) {
        update_option('new_theme_options', $_POST['footer_text']);
    }

////$theme_options = $new_values;

?>

<div class="wrap">
  <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme   Options', 'responsivetheme' ) . "</h2>"; ?>
  <?php $theme_options = get_option('new_theme_options',false); ?>
  <?php echo 'Existing Theme Option: '; print_r($theme_options); ?>
  <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; ?>" />
    <p class="submit">
      <input name="submit" type="submit" class="button-primary" id="submit" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
    </p>
  </form>
</div>
<?php
}
?>

I have also attached zip folder for the same. Please check and let me know.

Thanks,
Ajay

I have WP_DEBUG enabled , and i am using it localhost.

here is the errors

Notice: has_cap was called with an argument that is deprecated since version 2.0! Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead. in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 3006

Notice: Undefined index: footer_text in C:\xampp\htdocs\wordpress\wp-content\plugins\ajaytest\index.php on line 30

Notice: get_current_theme is deprecated since version 3.4! Use wp_get_theme() instead. in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 2908

And this error is from footer.php where the funcions is called.

Warning: Illegal string offset 'footer_text' in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\footer.php on line 11

This is my updated code and I am get rid of my old problem, I have new problem.

take look.

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 (
            'facebook_url' => 'http://www.facebook.com',
            'footer_text' => ''
        ));
        $theme_options = get_option('new_theme_options');
    }

    function theme_options_add_page() {
        add_theme_page( __( 'Theme Options', 'responsivetheme' ), __( 'Theme Options', 'responsivetheme' ), 'edit_theme_options', 'theme_options', '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 $theme_name = wp_get_theme() ?>
            <?php screen_icon(); echo "<h2>" . $theme_name . __( ' Theme Options', 'responsivetheme' ) . "</h2>"; ?>

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

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

            <p class="submit">
                    <input type="submit" class="button-primary" id="submit" value="<?php _e( 'Save Options', 'responsivetheme' ); ?>" />
                </p>
            </form>
        </div>
        <?php
    }

I have problem now with this code.

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;
        } 

I want to add more values in "$new_values" array.

but that is not working correct.

how can you add more values in this array and that working too and also update in Database too. eg.

$new_values = array (
        'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
        'faceobook_url' => '',
        'twitter_url' =? ''
        );

Hi,

To add multiple values for single variable we need to add as array. But we can't store php array values directly on mysql table. We need to change saving and retriving method. First convert all array values into string using serialize() function and retrive value using unserialize() function.

//Add to database
$new_val = array('footer'=>'footer value','fburl'=>'http://facebook.com/amilextech','twitter'=>'ajaygokhale')

update_option('new_theme_options', serialize($new_values));

//Retrive from database
$theme_options = get_option('new_theme_options');
$theme_values = unserialize($theme_options);

//To check values
print_r($theme_values);

Please check and let me know,
Thanks,
Ajay

I want to confirm that is the first variable of array $new_val has correct spells or it is $new_values?

This is the error.

Notice: Undefined variable: new_values in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php on line 350

Hi,

you are correct. This is my typo mistake. you can read as
update_option('new_theme_options', serialize([array or values]));
Updated Code like:

$new_values = array('footer'=>'footer value','fburl'=>'http://facebook.com/amilextech','twitter'=>'ajaygokhale')

update_option('new_theme_options', serialize($new_values));

Thanks,
Ajay

But its not working :(

how this could work correct in form and update save options too?

this is the error in <input/> value <br /><b>Warning</b>: Illegal string offset 'footer' in <b>C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php</b> on line <b>369</b><br />a

This is the input code:

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

Hi,

I have checked and update the code. To Save into database update the following code.

if( isset($_POST['submit']) ) {
    $new_values = array('footer_text'=>$_POST['footer_text'],'fburl'=>'http://facebook.com/amilextech','twitter'=>'ajaygokhale');
    update_option('new_theme_options', serialize($new_values));     
}

For Retriving use following code:

<?php 
    $theme_options = get_option('new_theme_options',false); 
    $theme_values = maybe_unserialize($theme_options);
?>

i.e. echo $theme_values['footer_text'] to display value.

I have also attach my file for your reference. Please check.

Thanks,
Ajay

This is updated code as you said

<label for="footer_text">Footer Text:</label>
    <input id="footer_text" type="text" name="footer_text" value="<?php echo $theme_values['footer_text']; ?>" />
<br />
<label for="fb">Facebook URL</label>
<input type="text" id="fb" name="fb" value="<?php echo $theme_values['fburl']; ?>" />

and this is the error
<br /><b>Notice</b>: Undefined index: footer_text in <b>C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\functions.php</b> on line <b>369</b><br />

this is footer updated code

<?php $theme_options = get_option('new_theme_options',false); $theme_values = maybe_unserialize($theme_options); echo $theme_values['footer_text']; ?>

and here is its error

Notice: Undefined index: fbrul in C:\xampp\htdocs\wordpress\wp-content\themes\responsive-theme\footer.php on line 11

here is the screeshot

Click Here

Hi,
Undefined index: footer_text i.e. we are not inserting the variable 'footer_text' at the time of saving.

Are you update the saving part?

if( isset($_POST['submit']) ) {
    $new_values = array('footer_text'=>$_POST['footer_text'],'fburl'=>'http://facebook.com/amilextech','twitter'=>'ajaygokhale');
    update_option('new_theme_options', serialize($new_values));     
}

Please check and let me know.
Thanks,
Ajay

What is difference in this code?

are you also following this code too?

if ( get_option('new_theme_options')) {
        $theme_options = get_option('new_theme_options');
    } else {
        add_option('new_theme_options', array (
            'facebook_url' => 'http://www.facebook.com',
            'footer_text' => ''
        ));
        $theme_options = get_option('new_theme_options');
    }

and the facebook url also not saving.

Hi,

I have Already comment above code. This code is not properly placed. Please take a look of my code. If possible try to run my code and check.

May be first time you will get error but after saving the value. you will get the correct result.

For your reference I will attached same code again. If possibel please use print_r() function to check output.

Thanks,
Ajay

should i use this code into theme_options_do_page() function?

I am using your same code but its not working.

see the screenshot.

Click Here

Hi,

I saw your screen shot. Can you please confirm the variable names?
i.e.
a) footer_text
b) fburl
c) twitter

because in your screen shot print_r() return [footer] and I think you are using ['footer_text']

also give me some time up to tomorrow, because today I am busy in another task.

Note: If you are getting this error first time, then please try to save new value and and check.

I have update code for just one value i.e. 'footer_text'. fburl and twitter are hard coded. Please make proper changes at saving. you need to change following line

$new_values = array('footer_text'=>$_POST['footer_text'],'fburl'=>'http://facebook.com/amilextech','twitter'=>'ajaygokhale');

Keep in touch.
If you have any issue, Please let me know.

Thanks,
Ajay

OK i have now changed my footer value from echo $theme_values['footer_text'];

to echo $theme_values['footer'];

and its not returing error now.

but its not saving new values to DB or any save changes.

ok,
Please send me your file / code as attachment, So I will try from my side.

Thanks,
Ajay

Here is the attachment.

Hi,
I have fixed the issue. You need to take care of variable names. In attached zip file you will find index.php (My File) and Theme-options.php (Your File). If you get error first time, then please ignore and updte the new values. After saving values in database it will work fine.

Please check and let me know.
Thanks,
Ajay

Hi, Is it your issue is resolved?

yes, this is resolved. But I have now another issues. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.