Advertisement
danhgilmore

Custom auth

Jun 24th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. /*
  2. I need to make sure every page visit is done so by an authorized logged in user. The problem is that every minute something is hitting the app, not logged in, so it's logging "Error - Not a user object".
  3.  
  4. How do I:
  5.  
  6. 1 - Stop this or
  7. 2 - Figure out who/what is firing off this code?
  8.  
  9. */
  10. function authlog($text)
  11. {
  12.     $authlog = ABSPATH . 'authlog.txt';
  13.     file_put_contents($authlog, date("Ymd - h:i:s - ") . "$text\n", FILE_APPEND);
  14. }
  15.  
  16. add_action('after_setup_theme', 'DHG_auth_init');
  17. function DHG_auth_init()
  18. {
  19.     if ( !is_user_logged_in() )
  20.     {
  21.         $user_obj = DHG_wp_authenticate();  // WP_User on success, FALSE on failure
  22.         if (!$user_obj instanceof WP_User)
  23.         {
  24.         authlog('Error - Not a WP_User object');
  25.         }
  26.        
  27.         $creds = array();
  28.         $creds['user_login']    = $user_obj->user_login;
  29.         $creds['user_password'] = 'plaintxtpw';
  30.         $creds['remember']      = true;
  31.         $user = wp_signon($creds, false);
  32.         if (is_wp_error($user))
  33.         {
  34.             authlog($user->get_error_message());
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement