Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active August 13, 2019 17:32
Show Gist options
  • Save cferdinandi/6608545 to your computer and use it in GitHub Desktop.
Save cferdinandi/6608545 to your computer and use it in GitHub Desktop.
Simple functions to set and retrieve alert messages in a browser session in WordPress. (Find and replace `_s_` with your own namespacing.)
<?php
// Start browser session (to store alert messages)
function _s_start_browser_session() {
if(!isset($_SESSION)) {
session_start();
}
}
add_action('init', '_s_start_browser_session');
// Store alert message in browser session
function _s_set_alert_message( $category, $code, $message ) {
$_SESSION[$category][$code] = $message;
}
// Get alert message from browser session
function _s_get_alert_message( $category, $code ) {
$message = '';
if ( isset($_SESSION[$category][$code]) && $_SESSION[$category][$code] != '' ) {
$message = $_SESSION[$category][$code];
unset($_SESSION[$category]);
}
return $message;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment