On my site, I show an alert box for important announcements. When the user dismisses this alert, a cookie is set that should prevent the same alert from being shown again.
Is it possible to use page caching in this situation?
The following code is included from functions.php
:
function xx_alert_box() {
$content = get_page(2);
$hash = md5($content->post_content);
if ((string) $_COOKIE['alerted'] === $hash) {
// These match, so don't show the alert
return;
}
// Only show the content if it's non-empty.
if (0 < strlen(trim($content->post_content))) {
$copy = apply_filters('the_content', $content->post_content);
require locate_template('alert.php');
wp_localize_script('xx', 'alerthash', $hash);
}
}
and then xx_alert_box()
is called from within header.php
. The alert.php
template just outputs the content of page 2.
So, I'd need 2 versions cached: the version with the alert on it, and the version without. Choosing between them would depend on the 'alerted' cookie's value.
Is that possible?