个性化阅读
专注于IT技术分析

如何在WordPress中获取用户激活URL?

对于以下代码:

add_action( 'user_register', 'my_user_register', 10, 1 );
function my_user_register($user_id){
   // how do i get user activation url
   // todo: send user an activation email
}

我想向用户发送激活电子邮件, 但是我不知道如何获取激活URL。请指教。


#1


你可以像这样发送电子邮件激活链接。

global $theme_settings;
$hidDiv="none";
$login_page_url = get_permalink($theme_settings['userlogin_page']);
$home_url = get_option('siteurl');;

$firstname         =  sanitize_text_field($_POST['firstname']);
$username         =  sanitize_text_field($_POST['email']);
$password         =  sanitize_text_field($_POST['password']);
$email            =  sanitize_text_field($_POST['email']);
$lastname         =  $_POST['lastname'];        
$company          =  $_POST['company'];
$city             =  $_POST['city'];
$state            =  $_POST['state'];       
$zipcode          =  $_POST['zipcode'];
$mailemail       = $_POST['mailingaddress'];

$user_id          =  wp_create_user( $username, $password, $email );
if ( is_wp_error( $user_id ) ) {
    $error_string = $user_id->get_error_message();
}else
{
    theme_user_register($user_id);
    $thanks_msg_page_url = get_permalink($theme_settings['userthanks_page']);
    $trackcode = get_user_meta($user_id, 'p_user_registration_code', true);
    $site_name = get_option('blogname');
    $track_url = get_option('siteurl').'/confirmation?trackid='.$user_id.'&trackcode='.$trackcode;          
    $headers = "MIME-Version: 1.0";
    $headers .= "Content-Type: text/html; charset=UTF-8";
    $headers .= "From: $site_name < $from >" . "\r\n";  
    $message = "<p> Hello ".$username;
    $message.="<p>Thank you for registering with us. Please click on the below link or copy and paste to the browser to activate your account.</p>";
    $message.="<p> $track_url</p><br>Best Regards, <br>Support Team";// Need to change this
    $from = get_option('admin_email');
    $site_name = get_option('blogname');

    wp_mail($email, 'Registration Confirmation', $message, $headers, '');
    $thanks_msg_page_url = get_permalink(theme_settings['userthanks_page']);
    wp_redirect($thanks_msg_page_url);
    exit;
}

#2


我不确定我是否理解为什么要通过编码获取激活URL, 除非你当然要自定义WordPress发送的默认新用户通知电子邮件。

如果你只想自定义在成功注册用户后发送给新用户的通知电子邮件, 则应使用过滤器wp_new_user_notification_email-自定义发送给用户的电子邮件和/或wp_new_user_notification_email_admin-自定义发送给管理员的电子邮件。

这两个功能都在” wp-includes / pluggable.php”中。过滤器定义如下…

/**
         * Filters the contents of the new user notification email sent to the site admin.
         *
         * @since 4.9.0
         *
         * @param array   $wp_new_user_notification_email {
         *     Used to build wp_mail().
         *
         *     @type string $to      The intended recipient - site admin email address.
         *     @type string $subject The subject of the email.
         *     @type string $message The body of the email.
         *     @type string $headers The headers of the email.
         * }
         * @param WP_User $user     User object for new user.
         * @param string  $blogname The site title.
         */
        $wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname );

…和…

/**
     * Filters the contents of the new user notification email sent to the new user.
     *
     * @since 4.9.0
     *
     * @param array   $wp_new_user_notification_email {
     *     Used to build wp_mail().
     *
     *     @type string $to      The intended recipient - New user email address.
     *     @type string $subject The subject of the email.
     *     @type string $message The body of the email.
     *     @type string $headers The headers of the email.
     * }
     * @param WP_User $user     User object for new user.
     * @param string  $blogname The site title.
     */
    $wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname );

典型的过滤器功能代码块如下所示:

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $wp_new_user_notification_email['subject'] = 'Your Subject';
    $wp_new_user_notification_email['message'] = 'Your Email Message';
    return $wp_new_user_notification_email;
}

在这些过滤器的add_filter函数内部, 你可以使用传递给过滤器函数的$ user获取特定于用户的信息, 例如…

$user_name = $user->user_login;
$user_email = $user->user_email;

使用$ user对象, 你可以获得用户信息并建立自己的激活链接。

同样, 我不确定我是否正确地遵循了这个问题的动机。但这是我从事这类工作的道路。

赞(0)
未经允许不得转载:srcmini » 如何在WordPress中获取用户激活URL?

评论 抢沙发

评论前必须登录!