I am trying to write events like current user's ip, username, login time to a log file. I have built the functions in php using WordPress hooks for the various events but how to bring them all together to my log function i don't know. These are my codes:
global $active_user;
/**
* function to get loggedin user's username
*/
add_action('init', 'log_file_setup');
function log_file_setup(){
$path = dirname(__FILE__) . '/log.txt';
$file = fopen($path,"w+");
$person = "John\n";
file_put_contents($path, $person, FILE_APPEND );
}
/**
* function to get loggedin user's username
*/
function get_username(){
if(is_user_logged_in()){
$active_user = wp-get-current-user();
$username = $active_user->login;
return $username;
}
}
/**
* function to get loggedin user's Role(s)
*/
function get_userole(){
if(is_user_logged_in()){
$active_user = wp-get-current-user();
// I have cast this into an array because the user may have multiple roles
$userroles = ( array ) $active_user->roles;
return $userroles;
}
}
I am trying to write events like current user's ip, username, login time to a log file. I have built the functions in php using WordPress hooks for the various events but how to bring them all together to my log function i don't know. These are my codes:
global $active_user;
/**
* function to get loggedin user's username
*/
add_action('init', 'log_file_setup');
function log_file_setup(){
$path = dirname(__FILE__) . '/log.txt';
$file = fopen($path,"w+");
$person = "John\n";
file_put_contents($path, $person, FILE_APPEND );
}
/**
* function to get loggedin user's username
*/
function get_username(){
if(is_user_logged_in()){
$active_user = wp-get-current-user();
$username = $active_user->login;
return $username;
}
}
/**
* function to get loggedin user's Role(s)
*/
function get_userole(){
if(is_user_logged_in()){
$active_user = wp-get-current-user();
// I have cast this into an array because the user may have multiple roles
$userroles = ( array ) $active_user->roles;
return $userroles;
}
}
Share
Improve this question
asked Sep 17, 2019 at 15:27
LandryLandry
111 bronze badge
2
|
1 Answer
Reset to default 0If you set the WP_DEBUG*
constants in your wp-config.php
file:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
// prevents debugging messages from appearing in the browser
...then you should be able to use error_log()
in your code, and your debugging messages will be saved in wp-content/debug.log
. (You'll probably have to ensure that wp-content/debug.log
exists and is writable by the server to make this work.)
See this answer: https://stackoverflow/a/55515556/1094518
wp-get-current-user()
you must usewp_get_current_user()
. – Johansson Commented Sep 17, 2019 at 16:00