Session alternative for plugins (due to caching)

I'm building a plugin that picks a random post from the database and delivers the result dynamically to the user. For certain aspects I need to know the post ID that's been randomly selected. Session data would work perfectly for this if it weren't for website caching. When a site is cached, the post that is supposed to load dynamically, based on the session data, doesn't load anything.

What alternatives do I have to using $_SESSION?

Topic session cache plugins Wordpress

Category Web


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.


Load your random posts through iframe. Caching plugin will catch your iframe url and don't use caching in randomposts.php or exclude it. Have you seen ads that changes every time, it does not depends on caching or no caching.


as a plugin author, caching is out of your control. you can make your plugin integrate with the leading caching plugins to serve dynamic content, but the whole idea of preventing caching while the owner of the site wants it sounds iffy.

The tradeoff of caching is that your sites content is not fully dynamic and if your plugin need to generate fully dynamic content therefor it is just incompatible and your plugin can not work with cached sites.

In other word, your only real option is to instruct the site owner how to disable his cache to enable your plugin's functionality


A similar question popped at [wp-hackers] and apparently Eric Mann's plugin, WP Session Manager, is the way to go.

I haven't analyzed the plugin's code, so cannot tell about its inner workings.

Pippin Williamson embeds WPSM in his Easy Digital Downloads plugin. Given Pippin's review and Eric being such a pro-old-timer-wp-ninja, I wouldn't look no further. Of course, a canonical Answer in Stack Exchange standards requires a full technical spec on what goes on, but I'll let this to another taker.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.