0

Here's my problem:

if ( !class_exists( 'printSecurity' ) ) {

    class printSecurity {

        public $constant_name_prefix = 'PRNSEC_';

        public function __construct() {

            define( $this->constant_name_prefix .'ROOTDIR', plugin_dir_path(__FILE__) );
            add_action('admin_menu', array( $this, 'printSecurityMenu') );
            add_action( 'wp_login', array( $this, 'last_login'), 10, 2 );
            add_shortcode('lastlogin', array( $this, 'wpb_lastlogin') );

        }

        public function printSecurityMenu() {

            add_menu_page('Print security List',
                'Print security Crud',
                'manage_options',
                'printSecurityMain',
                array($this, 'printSecurityMain')
            );

        }

        public function last_login( $user_login, $user ) {
            update_user_meta( $user->ID, 'last_login', time() );
        }

        public function wpb_lastlogin() { 
            $user = wp_get_current_user();
            $last_login = get_the_author_meta('last_login');
            var_dump($lastlogin);
            //die();
            $the_login_date = date('M j, Y h:i:s a', $last_login);


            if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
            //return apply_filters( 'wpb_get_ip', $ip );
            var_dump($last_login);
            $user_cool = [
                'user_login'    => $user->user_login,
                'user_id'       => $user->ID,
                'user_ip'       => $ip,
                'user_log'      => $the_login_date,
                'user_rule'     => $user->roles[0],
            ];
            echo "<ul>";
            foreach ($user_cool as $value) {
                echo '<li>'.$value.'</li>';
            }
            echo "</ul>";
            //require_once(PRNSEC_ROOTDIR . 'printSecurityMain.php');
        }

        public function printSecurityMain() {

            require_once(PRNSEC_ROOTDIR . 'printSecurityMain.php');

        }

    }

}

global $printSecurity;
$printSecurity = new printSecurity();

This code only works if I use the shortcode API, but ideally, I want to fire it before the shortcode (page), as soon as I will get the admin page I want to display the access and data.

I have a couple of questions about it:

  1. wp_login takes two arguments, $user_login and $user. Where do they come from?

  2. If I will try to call the second function differently, for example:

    add_menu_page('Print List', 'Print Crud', 'manage_options', 'wpb_lastlogin', array($this, 'wpb_lastlogin') );

And at the end of the wpb_lastlogin function

require_once(ROOTDIR . 'myadmintemplate.php');

and I will avoid adding the shortcode, it will return:

date() expects parameter 2 to be int, string given

Since $last_login will return NULL.

Is there something wrong that I do not quite understand about the last_login() function?

  • Are you trying to display the last login of the current user, the current author, or a particular user? – Tom J Nowell Apr 10 at 15:11
  • Just the current user – user3833012 Apr 10 at 15:11
  • Ah, but the current author isn't always the current user. The current author is the author of the current post, not the current logged in user – Tom J Nowell Apr 10 at 15:12
  • How could it matter in this environment? I will post the entire code just to be more clear – user3833012 Apr 10 at 15:13
  • because lets say I am user A and I logged in this afternoon, and I view a post by user B, who logged in this morning. Your code will say that user B logged in this afternoon. The function will give different values depending on which post or page you're on even though you stated it's always the time the current logged in user last logged in that you wanted – Tom J Nowell Apr 10 at 15:14
1

You don't need to store that information, WordPress already does this, and it does this across multiple sessions.

If you log in on your phone, and on your PC, then go to your user in WP Admin, you'll see them listed with a button to log you out on the other devices.

Additionally, WordPress stores the IP, expiration, and time the session began.

Here's a simple snippet that will reveal this information in the user edit screen:

<?php

add_action( 'show_user_profile', 'show_sessions_debug' );
add_action( 'edit_user_profile', 'show_sessions_debug' );

function show_sessions_debug( $user ) { ?>
    <table class="form-table">
        <tr>
            <th><label for="user_sessions_debug"><?php _e("Sessions"); ?></label></th>
            <td>
                <pre><?php var_dump( wp_get_all_sessions() ); ?></pre>
            </td>
        </tr>
    </table>
    <?php
}

enter image description here

| improve this answer | |
  • It is brilliant, but it works with session instead of the login function – user3833012 Apr 10 at 15:48
  • What about this one? $user = wp_get_current_user(); $last_login = get_user_meta($user->ID, 'last_login'); $the_login_date = date('M j, Y h:i:s a', $last_login[0]); – user3833012 Apr 10 at 15:53

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.