1

I have created a custom register page which ask for email, username and a membership number of another club.

Before the user is created, I am using an api to check the new user is an existing member of the club, and receiving the id number of that membership record.

I want to store this with the Wordpress user, so I am saving it in meta data.

This is a multisite, so am using the following code.

$user_meta = array('add_to_blog' => get_current_blog_id(), 'new_role' => 'subscriber', 'club_id' => $club_id);

\wpmu_signup_user($Username, $Email, apply_filters( 'add_signup_meta', $user_meta ) );

This all works and I can see my data in the wp_signups table and confirmation email is sent.

Using the Unconfirmed plugin I can also see my new user waiting for confirmation.

I have then set up this so that when the account is confirmed, the club_id is also transferred to the user meta details.

function activate_user($user_id, $password, $meta)   {
    if (isset($meta["club_id"]))
        update_user_meta( $user_id, "club_id", $meta["club_id"]);    
}

add_action('wpmu_activate_user','activate_user',10,3);

This works as expected if I activate the account using Unconfirmed plugin, but if the user uses the link in the email, it does not. The user is created, all emails send, but the club_id is not stored in the user meta because this hook is not being called.

Is there a way I can get round this?

  • 1
    I don't know this code, but I'd guess that wp-activate somehow isn't loading your plugin that adds the hook. So maybe putting your hook in a mu-plugin might help, if you haven't already? If not, try debugging what wp-load is actually loading from wp-activate, add trace to see if your hook is being registered, if it's being called, what the $meta value is etc. – Rup Feb 13 '20 at 9:51
  • Thank you - this gives me something to work with. I will have a look and comment back. – StripyTiger Feb 14 '20 at 10:49
  • @Rup - thanks. Using a must use plugin has made this work. If you want to add it as an answer, I will accept it. Thanks again. – StripyTiger Feb 19 '20 at 12:10
1

As discussed in the comments the fix is to put your wpmu_activate_user hook into a must-use plugin under /wp-content/mu-plugins/.

The problem is that the email's activation link handler, wp-activate.php, defines

define( 'WP_INSTALLING', true );

(I guess to cover the case it's running in the context of a newly provisioned site?) and wp-load will not load standard plugins if this is set: wp_get_active_and_valid_plugins() returns an empty array.

Hence your hook wasn't being registered in the wp-activate case. So to ensure this code is loaded and run we have to use a mu-plugin instead, which are loaded unconditionally even in the installing case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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