2017-03-17 45 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'); 
}