Tạo trường meta người dùng wordpress

Khi bạn đang tập trung vào các tác vụ cần quản lý người dùng và cần thêm nhiều trường cho người dùng. Trong trường hợp đó, ở đây chức năng meta người dùng được sử dụng. Điều này tương tự như việc tạo siêu dữ liệu bài đăng mới, nơi bạn có thể tạo các mẩu dữ liệu mới và đính kèm chúng vào bài đăng của mình, siêu dữ liệu của người dùng có thể hoạt động theo cùng một cách

Trong cơ sở dữ liệu WordPress, có một bảng được gọi là

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

6 lưu trữ tất cả thông tin bổ sung được đính kèm với hồ sơ người dùng. Bảng này có một ID duy nhất, ID người dùng, tên meta và các cột giá trị meta. Cấu trúc của bảng này có nghĩa là bạn có thể thêm bao nhiêu trường bổ sung vào tài khoản người dùng theo yêu cầu

Trong bài viết này, chúng tôi sẽ thêm các trường tùy chỉnh mới (liên kết xã hội) vào hồ sơ người dùng

Trong

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

7, có hai hành động tôi. e.
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

8 và
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

9

do_action( 'show_user_profile', $profileuser );

do_action( 'edit_user_profile', $profileuser );

Điều này sẽ cho phép chúng tôi sử dụng các hành động này để thêm nhiều trường hơn vào trang hồ sơ người dùng. Đoạn mã dưới đây sẽ cho phép chúng tôi thêm các trường mới mà chúng tôi có thể sử dụng để thêm các URL phương tiện truyền thông xã hội khác nhau cho người dùng

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

Đoạn mã trên sẽ thêm ba hộp nhập văn bản vào trang hồ sơ người dùng. Trong đoạn mã trên, hàm

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

0 WordPress được sử dụng. Hàm đó điền vào dữ liệu đã lưu hoặc trong các thế giới khác trả về siêu dữ liệu được lưu trữ đối với hồ sơ người dùng

Hàm

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

0 yêu cầu hai tham số đầu tiên là tên của trường, tham số thứ hai là ID của người dùng

Lưu các trường hồ sơ người dùng

Bây giờ, chúng ta cần thêm một hành động mới để lưu dữ liệu trường meta người dùng mới này, các hành động lưu mà chúng ta cần sử dụng là 

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

2 và 
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

3

Sau đó, chúng tôi có thể sử dụng biến toàn cầu

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

4 để lấy các trường đầu vào từ trang hồ sơ người dùng khi biểu mẫu được gửi và sau đó cập nhật chi tiết meta người dùng với các trường đầu vào này. Để cập nhật siêu dữ liệu người dùng, chúng tôi cần sử dụng chức năng WordPress
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

5

Theo cú pháp này, chúng ta có thể thấy hàm

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

5 có thể nhận 4 tham số

Đây là một mã ví dụ nơi chúng tôi đang lưu các liên kết mạng xã hội trong các trường meta người dùng

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

1

Hiển thị trường người dùng

Nếu bạn muốn hiển thị thông tin meta này trong chủ đề Wordpress thì có hai chức năng có thể được sử dụng để lấy dữ liệu này, đầu tiên là

add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

7 và thứ hai là
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

8. Sự khác biệt duy nhất giữa hai chức năng này là
add_action( 'show_user_profile', 'erweb_add_extra_social_links' );
add_action( 'edit_user_profile', 'erweb_add_extra_social_links' );
 
function erweb_add_extra_social_links( $user )
{
    ?>
        

New User Profile Links

0 sẽ trả về dữ liệu và
0 sẽ lặp lại dữ liệu

Khóa Meta người dùng trong WordPress là gì?

Siêu chức năng của WordPress được được sử dụng để truy xuất tất cả thông tin duy nhất có sẵn của mỗi người dùng được gọi là siêu dữ liệu. Nó thường trả về một giá trị được lưu trữ trong cấu trúc dữ liệu mảng và nó chứa một giá trị duy nhất của siêu hàm dữ liệu người dùng.

Tại sao WP có bảng Usermeta?

Bảng wp_users trong cơ sở dữ liệu người dùng WordPress được thiết kế để chỉ lưu trữ thông tin cơ bản của người dùng. Để lưu trữ dữ liệu người dùng WordPress bổ sung , bảng wp_usermeta được sử dụng. Trường ID từ bảng người dùng và trường user_id từ bảng này được sử dụng để liên kết bản ghi của cùng một người dùng.