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.
Have you tried
__CLASS__
returns the class name, not the class instance, so it only works for static functions.__CLASS__
should be$this
in youradd_action
call. I think__CLASS__
is only used if the function isstatic