Wp redirect - reset password

时间:2019-03-12 作者:Jennifer

我卡住了。我正在从approve post功能重定向到建立密码的链接。这是可行的,但不需要用户。也就是说,打开重置密码页面,但我看到的错误是没有指定用户。

我是否调用了错误的变量?提前谢谢。

 add_action(\'init\', \'approve_post\', 10, 2 );

 add_action(\'edit_user_created_user\', \'approve_post\', 10, 2 );

 function approve_post($user, $notify) {

 if ( isset($_GET[\'approve\']) && isset($_GET[\'email\']) ) {
// have we all variable needed?
if ( empty($_GET[\'approve\']) || ! filter_var($_GET[\'email\'], FILTER_VALIDATE_EMAIL) ) return;
// get post
$post = get_post($_GET[\'approve\']);
// this post has an author?
if ( ! isset($post->post_author) ) return;
// is the post already published?
if ( $post->post_status == \'publish\' ) {
  wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), \'login\') . "");
  exit();
} elseif ( $post->post_status != \'pending\' ) { // was the post deleted by admin?
  return;
}
// get the author
$author = new WP_User($post->post_author);
// check author variable
if ( ! isset($author->user_email) ) return;
// verify email
if ( $_GET[\'email\'] != $author->user_email ) return;
// verify key
$key = get_post_meta( $post->ID, \'_approve_key\', true);
if ( $key != md5( $author->user_email . $post->ID ) ) return;
// ok, update status
$post_data = array(\'ID\' => $post->ID, \'post_status\' => \'publish\');
$update = wp_update_post($post_data);
// update failed...
if ( ! $update ) return;
// delete verify key
delete_post_meta($post->ID, \'_approve_key\');
// work finished, view the post
wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), \'login\') . "");
exit();
 } return $notify;
}

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

(修改后的答案)

代码的问题是密码重置链接/URL缺少正确的密码重置键和用户登录slug—这个$key (密码重置键)和$user_login (用户登录slug)如下所示,两者都缺失(即代码中未定义):

network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), \'login\')
因此,要解决问题:

替换此:

if ( $post->post_status == \'publish\' ) {
  wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), \'login\') . "");
  exit();
}
使用此选项:

if ( $post->post_status == \'publish\' ) {
  if ( ! $user = get_user_by( \'email\', $_GET[\'email\'] ) ) return;
  $key = get_password_reset_key( $user );
  wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), \'login\') . "");
  exit();
}
更换:

// work finished, view the post
wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), \'login\') . "");
使用此选项:

// work finished, view the post
$key = get_password_reset_key( $author );
wp_redirect("" . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($author->user_login), \'login\') . "");
请注意init hook不会将任何参数传递给回调函数(在您的示例中是approve_post()), 因此,您的代码应该是:

add_action( \'init\', \'approve_post\' );
所以approve_post() 函数应定义为不带任何参数:

function approve_post() // like this
function approve_post($user, $notify) // not this
并钩住init 只有行动就足够了;所以这是不必要的:

add_action(\'edit_user_created_user\', \'approve_post\', 10, 2 );
这也是不必要的—at the end 功能(关闭前}), 您无需返回任何内容:

return $notify; // just remove this