0

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?

  • Many ways if you would be more specific as per your needs - but unset($_SESSION['x']); can work .. or change session name session_name('new_session_name'). and that is without going to WP hackish solutions like options, user_meta , transient and stuff.. – krembo99 Nov 20 '13 at 2:10
  • How does renaming a session get me around the session not being called due to caching? – bob_hardy Nov 20 '13 at 2:21
  • because you would use it every time before calling session_start(). BTW. session_destroy() and session_cache_limiter() are other options.. but like said - need more specific details to target- – krembo99 Nov 20 '13 at 2:36
0

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.

  • Thanks, but I'm looking for a solution I can put in my plugin. I don't want to require my plugin users to install another plugin in order to use mine – bob_hardy Nov 20 '13 at 2:22
0

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

0

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.

0

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' => 'user@name.com' ); // 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.

  • if it is cached, there is no request coming to the server and the old post id (or whatever it is used to generate still lives in the cached page. it is also the same answer as @brasofilo ;) – Mark Kaplun Oct 10 '17 at 2:03
  • @MarkKaplun he don't specify what type of cache is referring to. I assumed it was sessions stripped from headers like WpEngine does. I updated my answer with more info. – chifliiiii Oct 10 '17 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.