0

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;
         }
        }
2
  • You also have a typo in your function's name. Instead of wp-get-current-user() you must use wp_get_current_user().
    – Johansson
    Commented Sep 17, 2019 at 16:00
  • oh thanks @jack
    – Landry
    Commented Sep 17, 2019 at 16:04

1 Answer 1

0

If 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.com/a/55515556/1094518

2
  • Hi Pat, am not writing an error log. its a custom log for the different events i mentioned above
    – Landry
    Commented Sep 17, 2019 at 16:03
  • The function's name is error_log(); you can write out any messages you like, error or not, using it.
    – Pat J
    Commented Sep 17, 2019 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.