This topic has 2 replies, 1 voice, and was last updated 8 years by vinz98.

  • Author
  • #100654
     vinz98
    Participant

    simply add this fine piece of code to your functions.php (preferrably in your child theme)

     

    COPY CODE
    /*
    Plugin name: Single user login
    Plugin URI: 
    Description:
    Author: Ben May
    Author URI: 
    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();
    }
    #100655
     vinz98
    Participant

    I am sorry somewhere the copy [aste went wrong. admin cab you please put this right for me?

    /*
    Plugin name: Single user login
    Plugin URI:
    Description:
    Author: Ben May
    Author URI:
    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();
    }

    #100783
     vinz98
    Participant

    Question: How can I send the user which has been logged out to the homepage instead of the wp login page?

    wp_redirect( wp_login_url() );

    What do I put in the piece wp_login_url to get sent to the homepage?

    this? wp_redirect( home_url() );

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Log in with your credentials

Forgot your details?