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

时间:2016-07-21 16:49:57

标签: wordpress wordpress-theming

代码如下:

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
}

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

2 个答案:

答案 0 :(得分:0)

您可以发送此类电子邮件激活链接。

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;
}

答案 1 :(得分:0)

我不确定我是否理解为什么要通过编码获取激活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对象,您可以获得用户信息并建立自己的激活链接。

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