Hướng dẫn wordpress edit user profile - wordpress chỉnh sửa hồ sơ người dùng

Nếu bạn đang làm một trang web có chức năng quản lý thành viên , thì ắt hẳn là các bạn sẽ muốn tạo bên ngoài font end có chức năng cho phép thành viên tự chỉnh sửa thông tin của họ mà không cần phải đăng nhập vào trang admin.

Hướng dẫn wordpress edit user profile - wordpress chỉnh sửa hồ sơ người dùng

Các bạn có thể dễ dàng tạo trang chỉnh sửa thông tin thành viên này rất đơn giản. Chỉ việc copy  file page.php (nằm tại folder tên theme các bạn đang dùng). Sau đó past ngay tại folder theme, lúc này chúng ta sẽ có một file mới là bản copy của file page.php. Sửa lại tên file mới này là page-profile.php (các bạn có thể đổi theo tên mà các bạn muốn).page.php (nằm tại folder tên theme các bạn đang dùng). Sau đó past ngay tại folder theme, lúc này chúng ta sẽ có một file mới là bản copy của file page.php. Sửa lại tên file mới này là page-profile.php (các bạn có thể đổi theo tên mà các bạn muốn).

Mở file page-profile.php ra, sau đó copy đoạn code sau và dán nó vào vị trí đầu tiên trong file :page-profile.php ra, sau đó copy đoạn code sau và dán nó vào vị trí đầu tiên trong file :

 $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
 
        else
 
            $error[] = __('The passwords you entered do not match.  Your password was not updated.', 'profile');
 
    }
 
 
 
    /* Update user information. */
 
    if ( !empty( $_POST['url'] ) )
 
        update_user_meta( $current_user->ID, 'user_url', esc_url( $_POST['url'] ) );
 
    if ( !empty( $_POST['email'] ) ){
 
        if (!is_email(esc_attr( $_POST['email'] )))
 
            $error[] = __('The Email you entered is not valid.  please try again.', 'profile');
 
        elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
 
            $error[] = __('This email is already used by another user.  try a different one.', 'profile');
 
        else{
 
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
 
        }
 
    }
 
 
 
    if ( !empty( $_POST['first-name'] ) )
 
        update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
 
    if ( !empty( $_POST['last-name'] ) )
 
        update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
 
    if ( !empty( $_POST['description'] ) )
 
        update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );
 
 
 
    /* Redirect so the page will show updated info.*/
 
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
 
    if ( count($error) == 0 ) {
 
        //action hook for plugins and extra fields saving
 
        do_action('edit_user_profile_update', $current_user->ID);
 
        wp_redirect( get_permalink() );
 
        exit;
 
    }
 
}
 
?>

Sau đó thay thế vòng lặp (the loop) bằng đoạn code sau :

 
    

0 ) echo '

' . implode("
", $error) . '

'; ?>

Bây giờ các bạn vào trang admin, tạo một page mới , rồi từ danh sách Template ở mục Page Attributes rồi chọn tên “User Profile ” rồi nhấnPublish để hoàn tất.Template ở mục Page Attributes rồi chọn tên “User Profile ” rồi nhấnPublish để hoàn tất.

Đó là tất cả những gì các bạn cần làm để có một trang Edit User Profile, ngoài ra các bạn còn có thể chèn thêm các filed khác như địa chỉ, số phone, giới tính… cho các thành viên bằng cách thêm các trường này vào form giống như những fields đã có.