Trong mặc định WordPress có hỗ trợ bạn một số trường nhập thông tin người dùng như Tên người dùng, Họ, Tên, Biệt hiệu, Trang web, Tiểu sử,. nhưng không phải trường nào cũng được hiển thị bên ngoài để mọi người có thể nhìn thấy
Thể hiện theo yêu cầu, mình xin hướng dẫn bạn cách tạo một trường thông tin tùy chọn cho người dùng và lấy giá trị của trường bất kỳ để hiển thị chủ đề bên ngoài
Can see before. Bộ lọc hướng dẫn
Trong bài này mình xin chia sẻ làm 3 bước chính như sau
- Tạo ô nhập dữ liệu cho người dùng và làm cho nó lưu vào cơ sở dữ liệu
- Hiển thị chủ đề bên ngoài với
/*
* LƯU DỮ LIỆU TỪ FIELD NHẬP VÀO
*/
add_action[ ‘personal_options_update’, ‘save_custom_user_field’ ];
add_action[ ‘edit_user_profile_update’, ‘save_custom_user_field’ ];/* Function để lưu field mà gửi vào database */
function save_custom_user_field[ $user_id ] {
if [ !current_user_can[ ‘edit_user’, $user_id ] ] { return false; }
update_user_meta[ $user_id, ‘diachi’, $_POST[‘diachi’] ];
update_user_meta[ $user_id, ‘facebook’, $_POST[‘facebook’] ];
}/*
* LƯU DỮ LIỆU TỪ FIELD NHẬP VÀO
*/
add_action[ ‘personal_options_update’, ‘save_custom_user_field’ ];
add_action[ ‘edit_user_profile_update’, ‘save_custom_user_field’ ];/* Function để lưu field mà gửi vào database */
function save_custom_user_field[ $user_id ] {
if [ !current_user_can[ ‘edit_user’, $user_id ] ] { return false; }
update_user_meta[ $user_id, ‘diachi’, $_POST[‘diachi’] ];
update_user_meta[ $user_id, ‘facebook’, $_POST[‘facebook’] ];
} - Add CSS
Và từ bước 1 đến bước 3 chúng ta sẽ viết code vào file functions. php trong chủ đề hoặc tự tạo plugin mới
Bước 1. Tạo ô nhập dữ liệu
Để tạo trường nhập dữ liệu, chúng ta sẽ sử dụng hook
2 và/*
* LƯU DỮ LIỆU TỪ FIELD NHẬP VÀO
*/
add_action[ ‘personal_options_update’, ‘save_custom_user_field’ ];
add_action[ ‘edit_user_profile_update’, ‘save_custom_user_field’ ];/* Function để lưu field mà gửi vào database */
function save_custom_user_field[ $user_id ] {
if [ !current_user_can[ ‘edit_user’, $user_id ] ] { return false; }
update_user_meta[ $user_id, ‘diachi’, $_POST[‘diachi’] ];
update_user_meta[ $user_id, ‘facebook’, $_POST[‘facebook’] ];
}
3 để có thể viết một biểu mẫu nhập thông tin ở bên dưới trang sửa thông tin người dùng. Ta has as after/*
* LƯU DỮ LIỆU TỪ FIELD NHẬP VÀO
*/
add_action[ ‘personal_options_update’, ‘save_custom_user_field’ ];
add_action[ ‘edit_user_profile_update’, ‘save_custom_user_field’ ];/* Function để lưu field mà gửi vào database */
function save_custom_user_field[ $user_id ] {
if [ !current_user_can[ ‘edit_user’, $user_id ] ] { return false; }
update_user_meta[ $user_id, ‘diachi’, $_POST[‘diachi’] ];
update_user_meta[ $user_id, ‘facebook’, $_POST[‘facebook’] ];
}
/*
* TẠO FIELD CHO USER
*//*–Kích hoạt function cho các hooks–*/
add_action[ ‘show_user_profile’, ‘add_custom_user_field’ ];
add_action[ ‘edit_user_profile’, ‘add_custom_user_field’ ];
/* Khởi tạo các field dữ liệu */
function add_custom_user_field[ $user ]
{ ?>
Thông tin tùy chọn
[function[d, s, id] { var js, fjs = d.getElementsByTagName[s][0]; if [d.getElementById[id]] return; js = d.createElement[s]; js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=446126555427820"; fjs.parentNode.insertBefore[js, fjs]; }[document, ‘script’, ‘facebook-jssdk’]];
Thông tin tùy chọn
|