Why does “get_option” pull in the older value in options.php, rather than the newer value, on submission of a form?

If your update_option call runs after you assign $posts1 and $posts2, then of course they’ll hold the “old” value, even if you sleep for eternity 😉

Either run the update earlier, or (re)assign the variables afterwards.

$posts1 = get_option( 'post1' );

echo get_option( 'post1' ); // [my_id]
echo $posts1; // [my_id]

update_option( 'post1', 'foobar' );

echo get_option( 'post1' ); // foobar
echo $posts1; // [my_id]

See how $posts1 gets left out of the update?

Thus, the script runs again, and (I think) the new values from the submission should now be displayed at the top. But it’s the old ones.

Because when a user POST’s the form the script simply runs again, but the values aren’t updated till the end of the script. I can see the behaviour you’re after, and trust me, it will function as intended if you place the update logic at the start of the script.

Leave a Comment