WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单

WordPress 新用户注册邮件链接提示「您的密码重设链接无效」

PCDotFan5年前

异常坑爹的问题,不专门写文章说明了。这BUG从3版本至今愣是没人修复……位置在:/wp-includes/pluggable.php,将对应代码改为如下:$message .= ‘<‘ . network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user->user_login), ‘login’) . “\r\n\r\n”; 。

 

4.9.1 版本下的无痛修复方法:

未经测试,理论可行,慎用。于 functions.php 添加:

if ( ! function_exists( 'wp_new_user_notification_fixed' ) ) :
	function wp_send_new_user_notifications_fixed( $user_id, $notify = 'both' ) {
		wp_new_user_notification_fixed( $user_id, null, $notify );
	}
endif;
if ( ! function_exists( 'wp_new_user_notification_fixed' ) ) :
	function wp_new_user_notification_fixed( $user_id, $deprecated = null, $notify = '' ) {
		if ( $deprecated !== null ) {
			_deprecated_argument( __FUNCTION__, '4.3.1' );
		}
		global $wpdb, $wp_hasher;
		$user = get_userdata( $user_id );
		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
		// we want to reverse this for the plain text arena of emails.
		$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
		if ( 'user' !== $notify ) {
			$switched_locale = switch_to_locale( get_locale() );
			/* translators: %s: site title */
			$message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
			/* translators: %s: user login */
			$message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
			/* translators: %s: user email address */
			$message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";
			$wp_new_user_notification_email_admin = array(
				'to'      => get_option( 'admin_email' ),
				/* translators: Password change notification email subject. %s: Site title */
				'subject' => __( '[%s] New User Registration' ),
				'message' => $message,
				'headers' => '',
			);
			$wp_new_user_notification_email_admin = apply_filters( 'wp_new_user_notification_email_admin', $wp_new_user_notification_email_admin, $user, $blogname );
			@wp_mail(
				$wp_new_user_notification_email_admin['to'],
				wp_specialchars_decode( sprintf( $wp_new_user_notification_email_admin['subject'], $blogname ) ),
				$wp_new_user_notification_email_admin['message'],
				$wp_new_user_notification_email_admin['headers']
			);
			if ( $switched_locale ) {
				restore_previous_locale();
			}
		}
		// `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
		if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
			return;
		}
		// Generate something random for a password reset key.
		$key = wp_generate_password( 20, false );
		/** This action is documented in wp-login.php */
		do_action( 'retrieve_password_key', $user->user_login, $key );
		// Now insert the key, hashed, into the DB.
		if ( empty( $wp_hasher ) ) {
			require_once ABSPATH . WPINC . '/class-phpass.php';
			$wp_hasher = new PasswordHash( 8, true );
		}
		$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
		$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
		$switched_locale = switch_to_locale( get_user_locale( $user ) );
		/* translators: %s: user login */
		$message  = sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
		$message .= __( 'To set your password, visit the following address:' ) . "\r\n\r\n";
		$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";
		$message .= wp_login_url() . "\r\n";
		$wp_new_user_notification_email = array(
			'to'      => $user->user_email,
			/* translators: Password change notification email subject. %s: Site title */
			'subject' => __( '[%s] Your username and password info' ),
			'message' => $message,
			'headers' => '',
		);
		$wp_new_user_notification_email = apply_filters( 'wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname );
		wp_mail(
			$wp_new_user_notification_email['to'],
			wp_specialchars_decode( sprintf( $wp_new_user_notification_email['subject'], $blogname ) ),
			$wp_new_user_notification_email['message'],
			$wp_new_user_notification_email['headers']
		);
		if ( $switched_locale ) {
			restore_previous_locale();
		}
	}
endif;

remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );
remove_action( 'network_user_new_created_user', 'wp_send_new_user_notifications' );
remove_action( 'network_site_new_created_user',   'wp_send_new_user_notifications' );
remove_action( 'network_site_users_created_user', 'wp_send_new_user_notifications' );
add_action( 'register_new_user',      'wp_send_new_user_notifications_fixed' );
add_action( 'edit_user_created_user', 'wp_send_new_user_notifications_fixed', 10, 2 );
add_action( 'network_site_new_created_user',   'wp_send_new_user_notifications_fixed' );
add_action( 'network_site_users_created_user', 'wp_send_new_user_notifications_fixed' );
add_action( 'network_user_new_created_user',   'wp_send_new_user_notifications_fixed' );

 

另重置密码也出现了同样的问题,不过早有办法解决。位于主题目录 functions.php  添加:

/**
 * 修复 WordPress 找回密码提示“抱歉,该key似乎无效”
 * http://www.wpdaxue.com/lost-password-error-invalidkey.html
 */
function reset_password_message( $message, $key ) {
	if ( strpos($_POST['user_login'], '@') ) {
		$user_data = get_user_by('email', trim($_POST['user_login']));
	} else {
		$login = trim($_POST['user_login']);
		$user_data = get_user_by('login', $login);
	}
	$user_login = $user_data->user_login;
	$msg = __('有人要求重设如下帐号的密码:'). "\r\n\r\n";
	$msg .= network_site_url() . "\r\n\r\n";
	$msg .= sprintf(__('用户名:%s'), $user_login) . "\r\n\r\n";
	$msg .= __('若这不是您本人要求的,请忽略本邮件,一切如常。') . "\r\n\r\n";
	$msg .= __('要重置您的密码,请打开下面的链接:'). "\r\n\r\n";
	$msg .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') ;
	return $msg;
}
add_filter('retrieve_password_message', reset_password_message, null, 2);

 

#WordPress#wp-login
13
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
  • WP-Eloquent – 让 WordPress 像 Laravel 一样操作数据库
评论 (16)
再想想
  • 店小二

    因为这个问题的关键,不是WordPress 而是qq邮箱。老外自然不知道这个问题的存在了。无语啊

    5年前
  • 店小二

    我现在还在找怎么解决,您这个方法,可惜每次升级WordPress都要改啊。

    5年前
    • PCDotFan

      @店小二 已更新。

      5年前
  • 订货系统

    非常感谢这个提供的帮助,收藏了

    4年前
  • 胶质瘤

    很好,很强大

    4年前
  • 脑胶质瘤

    QQ邮箱,可以设置独立的单独密码,和QQ密码是独立的,两个密码不一样,该用哪个?

    3年前
  • 杂记
  • WordPress 付费主题推荐
Copyright © 2023 WP酷. Designed by nicetheme. 琼ICP备13002067号-1