plugins - How to write different events from different functions in my file to a log file in Wordpress

I am trying to write events like current user's ip, username, login time to a log file. I have built the functions

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
  • 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
Add a comment  | 

1 Answer 1

Reset to default 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/a/55515556/1094518

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745151949a4613910.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信