最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Wordpress Multisite user activation hook from email

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Feb 13, 2020 at 9:40 StripyTigerStripyTiger 2771 silver badge6 bronze badges 3
  • 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 Commented Feb 13, 2020 at 9:51
  • Thank you - this gives me something to work with. I will have a look and comment back. – StripyTiger Commented Feb 14, 2020 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 Commented Feb 19, 2020 at 12:10
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论