WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have built a WordPress plugin using classes. Each project class is created for a specific user. I want to do three things:

  • Keep the project object in memory for that user between his or her requests.
  • Delete it once the user signs out or after a certain expiration time.
  • Delete and recreate if user edits the project information (through a form) for that object.

Does anyone know how to do that?

share|improve this question

This is a perfect use case for Transients.

In WordPress, transients are short-lived data objects. By default, they're persisted to the database using WordPress' built-in WP_Object_Cache object. However, you can use a variety of caching plugins (Batcache is an outstanding one that works with Memcached) to store Transients in memory.

To set a transient, call set_transient( 'a unique name', 'the data value', 'time in seconds until it disappears' );

Your situation is working with a project object. Here's what you'd do:

function get_project( $user_id ) {
    if ( ! $project = get_transient( 'project_' . $user_id ) ) {
        // get_transient() returns false if the value is expired or doesn't exist.
        $project = new Project( $user_id );

        // Set the transient so that it lives for an hour
        set_transient( 'project_' . $user_id, $project, 3600 );

    return $project;

Then, in your code, you can call get_project() whenever you need to reference the current project. If it exists in the cache, you'll use the cached version. If it doesn't, you can build a new instance and save it to the cache. There's a lot more you'll need to do if you want to manipulate the object after you've retrieved it (and have those manipulations persist in the cache), but that's an exercise I leave to you.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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