简体   繁体   English

Wordpress user_meta_data注册

[英]Wordpress user_meta_data on registration

I haven't figured out how to add custom user_meta_data when the user is registered. 我还没有弄清楚在用户注册后如何添加自定义user_meta_data。

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 https://i.stack.imgur.com/FOcBP.png

I tried creating a plugin like ibenic suggested. 我尝试创建像ibenic建议的插件。

<?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/ . 您可以在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'. 当用户注册时,WordPress会调用一个操作:“ user_register”。

You can see also an example on it there: https://codex.wordpress.org/Plugin_API/Action_Reference/user_register 您还可以在此处看到一个示例: 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. 您可以使用user_register操作挂钩,该挂钩可让您在新用户添加到数据库后立即为其添加元数据。 The user id is passed to hook as an argument. 用户ID作为参数传递给hook。

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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