Wordpress 注册时的自定义用户字段
我正在为客户端开发一个自定义插件,我需要添加一些自定义用户字段。我翻遍了抄本,但找不到答案 我基本上需要在MySQL的users表中添加一些新行,然后在注册过程中添加一些额外的字段。我肯定还有其他插件可以让你添加自定义用户字段,但我想直接将其合并到我的插件中Wordpress 注册时的自定义用户字段,wordpress,Wordpress,我正在为客户端开发一个自定义插件,我需要添加一些自定义用户字段。我翻遍了抄本,但找不到答案 我基本上需要在MySQL的users表中添加一些新行,然后在注册过程中添加一些额外的字段。我肯定还有其他插件可以让你添加自定义用户字段,但我想直接将其合并到我的插件中 我该怎么做呢?我在WordPress回答中回答了一个类似的问题: 您需要操作挂钩register\u form(插入输入字段)和user\u register(处理它)。其余代码只是示例代码,用于检查页面配置文件和用户编辑中的结果 // R
我该怎么做呢?我在WordPress回答中回答了一个类似的问题: 您需要操作挂钩
register\u form
(插入输入字段)和user\u register
(处理它)。其余代码只是示例代码,用于检查页面配置文件和用户编辑中的结果
// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );
// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
function signup_fields_wpse_87261() {
?>
<label>
<input type="checkbox" name="custom_feature_a" id="custom_feature_a" />
Enable feature A?
</label>
<br />
<label>
<input type="checkbox" name="custom_feature_b" id="custom_feature_b" />
Enable feature B?
</label>
<hr />
<?php
}
function handle_signup_wpse_87261( $user_id, $data = null )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
if ( $feat_a )
{
add_user_meta( $user_id, 'custom_feature_a', $feat_a );
}
if ( $feat_b )
{
add_user_meta( $user_id, 'custom_feature_b', $feat_b );
}
}
function user_field_wpse_87261( $user )
{
$feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
$feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
<h3><?php _e('Custom Fields'); ?></h3>
<table class="form-table">
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
checked( $feat_a, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature A?'); ?></span>
</label>
</td>
</tr>
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
checked( $feat_b, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature B?'); ?></span>
</label>
</td>
</tr>
</table>
<?php
}
function save_profile_fields_87261( $user_id )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
update_usermeta( $user_id, 'custom_feature_a', $feat_a );
update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}
//注册
添加操作('register_form'、'signup_fields_wpse87261');
添加操作(‘用户注册’、‘处理注册’、‘wpse’87261’、10、2);
//侧面图
添加操作('show_user_profile'、'user_field_wpse87261');
添加操作(“个人选项更新”、“保存配置文件字段”87261);
//用户编辑
添加操作(“编辑用户配置文件”、“用户字段”wpse87261”);
添加操作(“编辑用户配置文件更新”、“保存配置文件字段”87261);
函数注册_字段_wpse_87261(){
?>
启用功能A?
启用功能B?
您使用的是默认注册还是自定义注册?不要触摸用户表,您需要user\u meta
进行注册。请立即进行默认注册。