我是php和web开发的新手。我为用户制作了一个前端编辑配置文件页面,每个字段都会更新,除了电子邮件(但其他所有内容都是用户元)。我在这件事上花了太长时间,但都没有用。
有很多关于这个的帖子,比如这篇How do you update user_email on the front end in WP 3.3?, 但我无法找到任何适合我的方法。我正在使用WP 4.7.3。
这是上面的帖子。它确实会更新电子邮件,但在我提交表单后,我的页面会无限期挂起:
wp_update_user( array( \'ID\' => $current_user->ID, \'user_email\' => $_POST[\'email\'] ) );
我也试过这种方法。它不会更新用户的电子邮件,也不会挂起:
$wpdb->update($wpdb->users, array(\'user_email\' => $_POST[\'email\']), array(\'ID\' => $current_user->ID));
如有任何建议,将不胜感激。我很确定问题就在于这段代码,但如果这有帮助的话,我可以包含更多的代码。
已编辑:更新用户配置文件代码。原来密码也会无限期挂起。
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' ) {
/* Update user information. */
// Email
if (isset( $_POST[\'email\'])) {
// check if user is really updating the value
if ($user_email != $_POST[\'email\']) {
// check if email is free to use
if (email_exists( $_POST[\'email\'] )){
// Email exists, do not update value.
$error[] = __(\'This email is already in use.\', \'profile\');
}
else if (!is_email( $_POST[\'email\'] )){
// Not correct email format.
$error[] = __(\'Email is in incorrect format.\', \'profile\');
}
else {
wp_update_user( array( \'ID\' => $current_user->ID, \'user_email\' => $_POST[\'email\'] ) );
//$wpdb->update($wpdb->users, array(\'user_email\' => $_POST[\'email\']), array(\'ID\' => $current_user->ID));
}
}
}
// Password
if ( !empty($_POST[\'pass1\'] ) && !empty( $_POST[\'pass2\'] ) ) {
if ( $_POST[\'pass1\'] == $_POST[\'pass2\'] )
wp_update_user( array( \'ID\' => $current_user->ID, \'user_pass\' => esc_attr( $_POST[\'pass1\'] ) ) );
else {
$error[] = __(\'Passwords do not match.\', \'profile\');
}
}
// First name
if ( !empty( $_POST[\'first-name\'] ) ){
update_user_meta( $current_user->ID, \'first_name\', esc_attr( $_POST[\'first-name\'] ) );
}
// Last name
if ( !empty( $_POST[\'last-name\'] ) )
update_user_meta($current_user->ID, \'last_name\', esc_attr( $_POST[\'last-name\'] ) );
// Birth year
if ( !empty( $_POST[\'birth_year\'] ) ) {
// Make sure it\'s all numbers
if (!ctype_digit($_POST[\'birth_year\'])){
$error[] = __(\'Please enter a year.\', \'profile\');
}
else
update_user_meta($current_user->ID, \'birth_year\', esc_attr( $_POST[\'birth_year\'] ) );
}
// Phone
if ( !empty( $_POST[\'phone\'] ) ) {
update_user_meta($current_user->ID, \'phone\', esc_attr( $_POST[\'phone\'] ) );
}
// Street address
if ( !empty( $_POST[\'address\'] ) )
update_user_meta($current_user->ID, \'street_address\', esc_attr( $_POST[\'address\'] ) );
// City
if ( !empty( $_POST[\'city\'] ) )
update_user_meta($current_user->ID, \'city\', esc_attr( $_POST[\'city\'] ) );
// State
if ( !empty( $_POST[\'state\'] ) )
update_user_meta($current_user->ID, \'state\', esc_attr( $_POST[\'state\'] ) );
// Parent\'s first name
if ( !empty( $_POST[\'p_first_name\'] ) )
update_user_meta($current_user->ID, \'player_parent_first_name\', esc_attr( $_POST[\'p_first_name\'] ) );
// Parent\'s last name
if ( !empty( $_POST[\'p_last_name\'] ) )
update_user_meta($current_user->ID, \'player_parent_last_name\', esc_attr( $_POST[\'p_last_name\'] ) );
// Parent\'s email
if (isset( $_POST[\'p_email\'])) {
// check if user is really updating the value
if ($user_email != $_POST[\'p_email\']) {
if (!is_email( $_POST[\'p_email\'] )){
// Not correct email format.
$error[] = __(\'Parent email is in incorrect format.\', \'profile\');
}
else {
$args = array(
\'ID\' => $current_user->id,
\'player_parent_email\' => esc_attr( $_POST[\'p_email\'] )
);
wp_update_user( $args );
}
}
}
// Parent\'s phone
if ( !empty( $_POST[\'p_phone\'] ) ) {
update_user_meta($current_user->ID, \'player_parent_phone\', esc_attr( $_POST[\'p_phone\'] ) );
}
/* Redirect so the page will show updated info.*/
// Make sure there are no errors
if ( count($error) == 0 ) {
echo "redirecting";
//action hook for plugins and extra fields saving
do_action(\'edit_user_profile_update\', $current_user->ID);
wp_redirect( get_permalink().\'?updated=true\' );
exit;
}
else {
// print errors here
}
}