2017-03-17 106 views
0

我还没有想到如何在用户注册时添加自定义的user_meta_data。注册时的Wordpress user_meta_data

我认为,代码如下:

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

我不知道是什么文件,这增加或者,或者如果它甚至正确的代码。

下面是元数据表中的测试用户元数据,它需要在注册时添加的条目。

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

我尝试创建一个插件像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); 
} 

?> 

我爱的插件的想法,我甚至没有想到这一点。谢谢。

+0

设置用PHP一个cookie –

回答

1

我会为此创建一个自定义插件。你可以在https://developer.wordpress.org/plugins/the-basics/了解更多。

然后在插件文件中,您可以添加您的功能。当用户注册时,WordPress会调用一个动作:'user_register'。

你也可以看到它的例子有: https://codex.wordpress.org/Plugin_API/Action_Reference/user_register

为了你自己的例子,你可以有这样的:

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

您可以使用user_register动作钩子,它允许您在新用户添加到数据库后立即添加元数据。用户标识作为参数传递给钩子。

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