4

For some context, we're building an activation system for new users of an app using WordPress as a framework. We've got a plugin driving most of our interactions, where all of this code resides.

When a new user signs up, they are sent an activate link via email, which when clicked, sends them to an activate page. When they save their details, we want to create the user with appropriate meta data, and redirect them to a custom routing template that shoots them off to their personal subsite.

We have the username and password of the newly created user available to us, so we can use wp_signon(). We've got a Registration_Model class that is instantiated somewhere else. Our class method to start the sign in process is where I believe we want to call add_action.

public function sign_user_in() {
    add_action( 'init', array( $this, 'auto_sign_in') );
}

Our method to process the sign-in is below, but be aware, I've tested out the function to see if the action is even being cued by WordPress by echoing a string, which does nothing.

public function auto_sign_in()
{
    // if ( ! is_user_logged_in() ) {

         //determine WordPress user account to impersonate
         $creds = array();
         $creds['user_login'] = $this->user->email;
         $creds['user_password']  = $this->user->password->one;
         $creds['remember'] = true;
         $user = wp_signon( $creds, false );
         if ( is_wp_error( $user ) ) {
             echo $user->get_error_message();
         } else {
             wp_redirect('/_router/'); exit;
         }
     }
}

It really appears that I'm stuck on the action not being run, or something. Can the add_action be run from somewhere other than the class constructor? Any and all advice or help is appreciated.

EDIT

I've adjusted the function to use $this instead of __CLASS__, although we still get nothing when hooking into 'init'. Just as a test, I've used the absolute last hook that fires, 'shutdown', and I can echo a test string to the browser using it. So I know that there's a whole bunch of operations loading, so I probably need to move the add_action somewhere else so it fires earlier.

2
  • where/when is sign_user_in called?
    – Milo
    Jan 30, 2013 at 18:07
  • sign_user_in is called in a private static function in the controller class. Jan 30, 2013 at 20:05

2 Answers 2

6

Have you tried

add_action( 'init', array( $this, 'auto_sign_in') );

__CLASS__ returns the class name, not the class instance, so it only works for static functions.

5
  • Please explain why that could solve the problem.
    – fuxia
    Jan 30, 2013 at 18:55
  • __CLASS__ returns the class name, not the class instance, so it only works for static functions.
    – vancoder
    Jan 30, 2013 at 19:07
  • What I meant was: Explain it in your answer, not in a comment. :)
    – fuxia
    Jan 30, 2013 at 19:09
  • Sorry, I'm a newbie.
    – vancoder
    Jan 30, 2013 at 19:12
  • Welcome to WordPress Stack Exchange!
    – fuxia
    Jan 30, 2013 at 19:15
3

__CLASS__ should be $this in your add_action call. I think __CLASS__ is only used if the function is static

1
  • 2
    Please, &$this is PHP 4. It is just $this nowadays. :)
    – fuxia
    Jan 30, 2013 at 18:57

Your Answer

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

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