Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Tùy biến thêm trường thông tin user meta – WordPress

Tùy biến thêm trường thông tin user meta – WordPress

Thứ Năm, 03/07/2014 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Để thêm user meta ngoài các user fields mặc định như: email,name,first_name,last_name,… Ở ví dụ này chúng ta sẽ thêm field phone.
Sử dụng action show_user_profile và edit_user_profile để hiển thị user fields.

//custom user profile
//show và edit page
add_action('show_user_profile', 'my_show_extra_profile_fields');
add_action('edit_user_profile', 'my_show_extra_profile_fields');
function my_show_extra_profile_fields($user)
{
	$phone=get_the_author_meta('phone',$user->ID);
	?>
	<table class="form-table">
            <tr>
                <td>phone number</td>
                <td><input type="text" name="phone" value="<?php echo $phone?>"/></td>
            </tr>
	</table>
	<?php
}

– Fields mới này chưa được lưu vào database, để lưu vào database sử dụng thêm đồng thời 2 action “personal_options_update” ,”edit_user_profile_update” để cập nhật fields.

//update user profile
add_action('personal_options_update', 'my_save_extra_profile_fields');
add_action('edit_user_profile_update', 'my_save_extra_profile_fields');

function my_save_extra_profile_fields($user_id)
{
	if (!current_user_can('edit_user', $user_id))
		return false;
	update_usermeta($user_id, 'phone', $_POST['phone']);    //update user profile
}

Plugin tạo avatar cho người dùng WP User Avatar là một ví dụ điển hình về cách tạo user meta field cho wordpress sử dụng tính năng này.
Cách khác bạn có thể tạo field cho user bằng cách tùy biến custom fields với plugin Advanced Custom Fields.

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: custom user meta, user data, user meta

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.