I'm not sure if it is true but seems that the content of the widgets is cached. I initially thought there was a problem with my code (I need to hide/show a section in a custom widget according to a session value) but I have the same problem with all widgets and get_option().
I did this test.
In page "A" I update an option:
update_option( 'custom_check', 'a' );
In page "B" I update the same option with a different value:
update_option( 'custom_check', 'b' );
When I go from the page "A" to "B" (or viceversa) the options are updated correctly in the database. And the value is printed correctly in the page with:
echo get_option( 'custom_check' );
But if I print the value in a widget (I've tried with different widgets, default text and recent posts widgets included) the value is not updated. Until the refresh of the page.
Example: I'm on page "A" so the value is "a". When I visit the page "B", the value is "b" in the database and is printed correctly in the page content. But the widget displays the old value ("a"). When I refresh the page, the widget displays the correct value ("b").
Any idea?
[Edit] I will try to explain better what I mean.
I’m using Twenty Fourteen, not a custom theme. In the sidebar I’ve the menu with page “A” and “B” so I can visit those pages with one click (@s_ha_dum♦ no AJAX involved). And the default text widget included in WordPress.
In page.php
(Twenty Fourteen) I have this code at the top:
<?php
if ( is_page( 749 ) ) {
update_option( 'custom_check', 'a' );
} elseif ( is_page( 800 ) ) {
update_option( 'custom_check', 'b' );
}
echo get_option( 'custom_check' );
?>
749 and 800 are the IDs of the pages “A” and “B”.
To test the variable in a widget, I’ve inserted this line in /wp-includes/default-widgets.php
(I know that it is bad practice, I’m just testing), inside the WP_Widget_Text class:
echo get_option( 'custom_check' );
The problem: I am on page “A”, “custom_check” equals to “a” and the widget displays “a”. When I click on the page “B” link on the menu, “custom_check” equals to “b” in the db and is printed correctly at the top of the page. But in the text widget I still see “a”. If I refresh the page, the widget display the correct value.
Is this a cache problem? Or maybe the update of the option fires too late (after widget_init)?