[wp-hackers] Using WP Transients for User Sessions

J.D. Grimes jdg at codesymphony.co
Wed Oct 16 16:30:20 UTC 2013


On Oct 16, 2013, at 12:22 PM, BenderisGreat <greglancaster71 at gmail.com> wrote:

> thats what I thought it was doing, but I wasnt sure because a var_dump looks
> like each session is stored via the user_id
> 
> array(2) { [1]=> int(1381939181) [13]=> int(1381940097) }
> 
> how would I go about calling delete_transient() from an array like that. On
> http://codex.wordpress.org/Function_Reference/delete_transient  it says it
> only accepts a single string. 

Don't call delete_transient(). Do something like this:

<?php

function clear_transient_on_logout() {

	$logged_in_users = get_transient( 'users_online' );
	
	$user_id = get_current_user_id();

	// Remove this user.
	unset( $logged_in_users[ $user_id ] );

	set_transient( 'users_online', $logged_in_users );
}
add_action('wp_logout', 'clear_transient_on_logout');

?>

-J.D.


More information about the wp-hackers mailing list