Nhận người dùng wordpress

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

  1. 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
  2. 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’] );
    }

    0, bạn vẫn có thể sử dụng

    /*
    * 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’] );
    }

    1 nếu thích. Làm trong bài này mình dùng filter để tránh đụng vào file đơn. php nên mình dùng get
  3. 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 

/*
* 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’] );
}

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


/*
* 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














Như trong code, mình đã tạo ra 2 trường với tên là diachi và facebook. Và tí nữa ta sẽ gọi nó ra là

/*
* 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’] );
}

0,…

Nhận người dùng wordpress

Nhưng cái đó chỉ là tạo thôi, chúng ta cần sử dụng thêm

/*
* 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’] );
}

1 để lấy giá trị đầu vào từ trường rồi gửi đến hàm

/*
* 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’] );
}

2 mà lưu lại các thông tin đó. Ta viết tiếp

/*
* 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’] );
}

Bây giờ bạn có thể thử nhập thông tin vào 2 trường vừa tạo và nhấn Lưu thay đổi, nếu nó lưu lại bình thường là coi như thành công ở bước 1

Bước 2. Showing into under content by Filter

Để tránh tối thiểu việc chỉnh sửa chủ đề trực tiếp, mình khuyến khích các bạn nên sử dụng Bộ lọc để hiển thị. Sau khi this has change theme thì chỉ cần Bốc tách đoạn mã ở trên và đoạn mã này đi là oke. . D


/* Hiển thị field dưới mỗi bài viết */

add_filter( ‘the_content’, ‘show_user_field’ );
function show_user_field($content) {

$user_field =’

’.get_avatar( get_the_author_meta(‘ID’), 85 ) .'


‘.get_the_author_meta(‘description’).’

  • Địa chỉ: ’.get_the_author_meta(‘diachi’).'



’;

if ( is_singular(‘post’) ) {
return $content.$user_field;
} else {
return $content;
}
}

Giải thích ngắn gọn là ở đoạn này chúng ta sử dụng hàm get_the_author_meta() để hiển thị thông tin của người dùng và bên trong đó là một trường chứa tham số mà bạn cần gọi ra. Nếu bạn muốn biết tham số của các trường mặc định này thì có thể tham khảo bên dưới

  • đăng nhập người dùng
  • Mật khẩu người dùng
  • user_nicename
  • Email người dùng
  • user_url
  • user_registered
  • user_activation_key
  • tâm trạng người dùng
  • tên hiển thị
  • tên nick
  • họ
  • họ
  • mô tả (Thông tin tiểu sử từ hồ sơ của người dùng)
  • nói huyên thuyên
  • mục đích
  • yim
  • user_level
  • user_firstname
  • user_lastname
  • rich_editing
  • bình luận_phím tắt
  • quản trị_color
  • plugins_per_page
  • plugins_last_view
  • TÔI

Dostep 2 mình có sử dụng nút Theo dõi của Facebook kết hợp với trường nhập liên kết Facebook nên ở đây mình phải thêm đoạn mã của Facebook để có thể hiển thị nó. Dùng action luôn nhé


add_action(‘wp_footer’, ‘tp_footer_scripts’);
function tp_footer_scripts() {?>



You can change

/*
* 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 to APP ID of you

Bước 4. Add CSS tí hon

Để hiển thị dưới dạng demo, bạn có thể thêm một vài đoạn CSS bên dưới vào kiểu tệp. css

________số 8

CSS này mình viết nhanh để làm demo nên không đảm bảo là nó thể hiện tốt ở mọi theme đâu nên tốt nhất bạn nên chỉnh sửa lại theo cho phù hợp với theme của mình nếu có xảy ra lỗi nhé

Ok bây giờ ta có kết quả là

Nhận người dùng wordpress

Không khó phải không nào? . php hoặc tự tạo một plugin nhé


/*
* 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














/*
* 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’] );
}

/*
* HIỂN THỊ FIELD DƯỚI BÀI VIẾT
*/
add_filter( ‘the_content’, ‘show_user_field’ );
function show_user_field($content) {

$user_field =’

’.get_avatar( get_the_author_meta(‘ID’), 85 ) .'


‘.get_the_author_meta(‘description’).’

  • Địa chỉ: ’.get_the_author_meta(‘diachi’).'



’;

if ( is_singular(‘post’) ) {
return $content.$user_field;
} else {
return $content;
}
}

Lời khuyên. Tạo trường nhanh chóng với plugin

Nếu bạn muốn tạo nhiều trường nhanh chóng, có thể sử dụng các plugin sau

  • Trường người dùng bổ sung
  • các loại

Chúc các bạn làm thành công

Cách gửi mã lên bình luận

Nếu bạn làm không được, hãy cho mình xem mã của bạn. Bạn nên vào Pastebin. com và dán mã của bạn vào đó, sau đó gửi liên kết ở bình luận