WordPress

wpdb — Zaktualizuj adres e-mail użytkownika przez php — WP 4.7

  • 15 marca, 2017
  • 4 min read
wpdb — Zaktualizuj adres e-mail użytkownika przez php — WP 4.7


Jestem nowy w php i tworzeniu stron internetowych. Stworzyłem front-endową stronę profilu edycji dla użytkowników, a wszystkie pola są aktualizowane z wyjątkiem wiadomości e-mail (ale wszystko inne to meta użytkownika). Spędziłem nad tym zbyt dużo czasu, bezskutecznie.

Istnieje wiele postów na ten temat, takich jak ten Jak zaktualizować adres e-mail użytkownika na interfejsie w WP 3.3?, ale nie mogę uzyskać żadnej z sugerowanych metod, aby działała dla mnie. Używam WP 4.7.3.

To z powyższego posta. Zaktualizuje wiadomość e-mail, ale moja strona zawiesza się na czas nieokreślony po przesłaniu formularza:

wp_update_user( array( 'ID' => $current_user->ID, 'user_email' => $_POST['email'] ) );

Próbowałem też w ten sposób. Nie aktualizuje adresu e-mail użytkownika i nie zawiesza się:

$wpdb->update($wpdb->users, array('user_email' => $_POST['email']), array('ID' => $current_user->ID));

Wszelkie porady byłyby bardzo mile widziane. Jestem prawie pewien, że problem leży tylko w tym fragmencie kodu, ale mogę dołączyć więcej kodu, jeśli to pomoże.

Edytowano: zaktualizuj kod profilu użytkownika. Okazuje się, że hasło też wisi na czas nieokreślony.

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
    }

}


Źródło

Warto przeczytać!  Jak przejść z NextGEN do Galerii Envira w WordPress