WordPress

How to add Custom User Meta values in database WordPress

0
codexjunction

In WordPress, you can add custom user meta values to the database to store additional information about users. These custom user meta values can be used for various purposes, such as storing user preferences, additional profile information, or any other data related to users. Here’s how you can add custom user meta values in WordPress:

**Method 1: Using Functions in Your Theme’s `functions.php` file**

1. Open your theme’s `functions.php` file. You can do this via the WordPress admin panel by going to “Appearance” > “Theme Editor.”

2. Add the following code to your `functions.php` file to create and update user meta:

// Function to add/update custom user meta
function add_custom_user_meta($user_id, $meta_key, $meta_value) {
update_user_meta($user_id, $meta_key, $meta_value);
}

// Hook this function to a WordPress action, for example, 'user_register' to add meta during user registration
add_action('user_register', 'add_custom_user_meta', 10, 3);

Replace `$meta_key` with your custom meta key (e.g., ‘custom_meta_key’) and `$meta_value` with the value you want to store.

3. Save the changes to your `functions.php` file.

4. To add custom user meta, you can use the `add_custom_user_meta` function. For example, to add the user’s favorite color when they register, you can do the following:

add_custom_user_meta($user_id, 'favorite_color', 'blue');

**Method 2: Using a Plugin**

If you prefer a user-friendly interface to add and manage custom user meta fields, you can use a plugin like “User Meta Manager” or “Profile Builder” that allows you to create and manage custom user fields without writing code.

Here’s how to add custom user meta fields using the “User Meta Manager” plugin:

1. Install and activate the “User Meta Manager” plugin from the WordPress Plugin Repository.

2. In your WordPress dashboard, go to “Users” > “User Meta Manager.”

3. Click on the “Add Field” button to create a new custom user meta field.

4. Configure the field settings, including the field type, label, and key. For example, you can create a “Favorite Color” field with a key like “favorite_color.”

5. Save the field, and it will be available to edit for each user profile.

6. To add or update the custom user meta values for individual users, go to “Users” > “All Users,” edit the user you want to add the custom meta for, and you’ll find the custom field in the user’s profile.

Both of these methods allow you to add and manage custom user meta values in the WordPress database. Choose the one that best suits your needs and level of technical expertise.

Facebook Social Login Error in WordPress

Previous article

Get Orignal IP while Using Cloudflare DNS in PHP

Next article

You may also like

Comments

Leave a reply

Your email address will not be published. Required fields are marked *

More in WordPress