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:
wp_login takes two arguments, $user_login and $user. Where do they come from?
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?
