Skip to content

Instantly share code, notes, and snippets.

@5iDS
Created June 8, 2016 10:11
Show Gist options
  • Save 5iDS/9274816d932c3fa52ed2058531637371 to your computer and use it in GitHub Desktop.
Save 5iDS/9274816d932c3fa52ed2058531637371 to your computer and use it in GitHub Desktop.
Wordpress Single Login Session Per User
<?php
/*
Plugin name: Single user login
Plugin URI:
Description:
Author: Ben May
Author URI: http://benmay.org/wordpress/single-user-wordpress/
Version: 0.1
*/
if( !class_exists( 'WPSingleUserLoggin' ) )
{
class WPSingleUserLoggin
{
private $session_id;
function __construct()
{
if ( ! session_id() )
session_start();
$this->session_id = session_id();
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_login', array( $this, 'wp_login' ), 10, 2 );
}
function init()
{
if( ! is_user_logged_in() )
return;
$stored_sess_id = get_user_meta( get_current_user_id(), '_wp_single_user_hash', true );
if( $stored_sess_id != $this->session_id )
{
wp_logout();
wp_redirect( wp_login_url() );
exit;
}
}
function wp_login( $user_login, $user )
{
update_user_meta( $user->ID, '_wp_single_user_hash', $this->session_id );
return;
}
}
new WPSingleUserLoggin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment