使用WordPress register_new_user() PHP action
记录新用户注册后,register_new_user WordPress PHP 操作将触发
推荐:[最新版]WordPress SEO插件Rank Math Pro
register_new_user() action基本语法
描述
记录新用户注册后触发
用法
add_action('register_new_user', 'your_custom_function');
function your_custom_function($user_id) {
// your custom code here
}
- $user_id(int) – 新注册用户的 ID。
推荐:Swift Performance插件教程WordPress缓存性能优化插件
register_new_user() action函数
记录新用户注册后触发(源文件可参考这里)
do_action( 'register_new_user', $user_id );
推荐:Max Mega Menu插件教程WordPress添加超级菜单
如何使用register_new_user()
向新用户发送欢迎电子邮件新用户成功注册后向其发送欢迎电子邮件。
add_action('register_new_user', 'send_welcome_email_to_new_user');
function send_welcome_email_to_new_user($user_id) {
$user = get_userdata($user_id);
$email = $user->user_email;
$subject = 'Welcome to our website!';
$message = 'Hello! Thank you for registering on our website. We hope you enjoy your experience.';
wp_mail($email, $subject, $message);
}
为新用户分配自定义角色根据新用户的电子邮件域为其分配自定义角色。
add_action('register_new_user', 'assign_custom_role_based_on_email');
function assign_custom_role_based_on_email($user_id) {
$user = get_userdata($user_id);
$email = $user->user_email;
$domain = substr(strrchr($email, "@"), 1);
if ($domain == 'example.com') {
$user->set_role('custom_role');
}
}
记录新用户注册记录所有新用户注册的用户 ID 和注册时间戳。
add_action('register_new_user', 'log_new_user_registrations');
function log_new_user_registrations($user_id) {
$logfile = fopen('registration_log.txt', 'a');
$log_entry = 'User ID: ' . $user_id . ', Registered at: ' . date('Y-m-d H:i:s') . "\n";
fwrite($logfile, $log_entry);
fclose($logfile);
}
向新用户添加元字段为新用户添加一个名为“signup_source”的自定义元字段,并设置其默认值。
add_action('register_new_user', 'add_signup_source_meta_field');
function add_signup_source_meta_field($user_id) {
$signup_source = 'default';
add_user_meta($user_id, 'signup_source', $signup_source);
}
向管理员发送新用户注册通知当新用户注册时通过电子邮件通知网站管理员。
add_action('register_new_user', 'send_admin_notification_new_user');
function send_admin_notification_new_user($user_id) {
$user = get_userdata($user_id);
$email = get_option('admin_email');
$subject = 'New user registration on your website';
$message = 'A new user has registered on your website with the following details: Username - ' . $user->user_login . ', Email - ' . $user->user_email;
wp_mail($email, $subject, $message);
}
Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折