简体   繁体   中英

Wordpress user_meta_data on registration

I haven't figured out how to add custom user_meta_data when the user is registered.

I think the code would be:

update_user_meta( $user_id, 'weight', 5 );

I don't know what file to add this to or where, or if it's even the right code.

Below is a test users meta data from the meta data table that would need the entry added at registration.

https://i.stack.imgur.com/FOcBP.png

I tried creating a plugin like ibenic suggested.

<?php
/**
 * @package Weight_Add
 * @version 1.0
 */
/*
Plugin Name: Weight Add
Description: Adds weight data on user registry
Author: Straconis
Version: 1.0
*/

add_action( 'user_register', 'my_add_post_meta' );

function my_add_post_meta( $user_id ) {
   // Do checks if needed
   update_user_meta( $user_id, 'weight', 5 );
}

?>

I love the idea of the plugin, I didn't even think of that. Thank you.

I would create a custom plugin for this. You can learn more about that at https://developer.wordpress.org/plugins/the-basics/ .

Then in the plugin file, you can add your functions. When user registers, WordPress calls an action: 'user_register'.

You can see also an example on it there: https://codex.wordpress.org/Plugin_API/Action_Reference/user_register

For your own example, you could have something like:

// 'my_add_post_meta' - your function that will hold the code
add_action( 'user_register', 'my_add_post_meta' );

function my_add_post_meta( $user_id ) {
   // Do checks if needed
   update_user_meta( $user_id, 'weight', 5 );
}

You may use user_register action hook which allows you to add meta data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.

add_action( 'user_register', 'save_upon_user_registration', 10, 1 );
function save_upon_user_registration( $user_id ) {
    update_user_meta($user_id, 'meta_key', 'meta_data');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM