You can integrate https://wordpress.org/plugins/wp-session-manager into your plugin very easily.
Simple download the plugin and copy all the files in the includes folder into your plugin. Then load them like this or create a wrapper around them:
// let users change the session cookie name
if( ! defined( 'WP_SESSION_COOKIE' ) ) {
define( 'WP_SESSION_COOKIE', '_wp_session' );
}
if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {
include 'includes/class-recursive-arrayaccess.php';
}
// Include utilities class
if ( ! class_exists( 'WP_Session_Utils' ) ) {
include 'includes/class-wp-session-utils.php';
}
// Include WP_CLI routines early
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include 'includes/wp-cli.php';
}
// Only include the functionality if it's not pre-defined.
if ( ! class_exists( 'WP_Session' ) ) {
include 'includes/class-wp-session.php';
include 'includes/wp-session.php';
}
Now you can use sessions like this:
$wp_session = WP_Session::get_instance();
$wp_session['user_name'] = 'User Name'; // A string
$wp_session['user_contact'] = array( 'email' => '[email protected]' ); // An array
$wp_session['user_obj'] = new WP_User( 1 );
You don't specify the type of cache. If you are talking about cache used by hosting like WpEngine, pagely, etc where they strip sessions you need to use the solution I commented here.
If you are referring to page cache from plugins like WpSuperCache, etc you need to use ajax to display the result once the page is rendered. That way you won't be affected by it.
unset($_SESSION['x']);
can work .. or change session namesession_name('new_session_name')
. and that is without going to WP hackish solutions like options, user_meta , transient and stuff..session_start()
. BTW.session_destroy()
andsession_cache_limiter()
are other options.. but like said - need more specific details to target-