我只是自己经历了整个过程。没有一个钩子或任何东西可以让这变得容易,因此我们必须将其“破解”到位,以使其工作。这并不漂亮,但它完成了任务。我已经在3个自定义字段中使用了几个月了,从一开始就添加自定义用户信息可以节省大量时间。
这将向管理仪表板添加自定义字段“Add New User
“页面。如果您正试图这样做,那么将此页面转换为公共注册页面应该不会太难。
将所有这些代码添加到主题函数中。php。
步骤1:将字段添加到添加用户页面:
// Add custom field for wp-admin add user page.
// There is no filter or hook, and this is a hack, but it works.
function wpse_135622_add_new_user_custom_field_hack(){
global $pagenow;
# do this only in page user-new.php
if($pagenow !== \'user-new.php\')
return;
# do this only if you can
if(!current_user_can(\'manage_options\'))
return false;
?>
<table id="table_custom_field_1" style="display:none;">
<!-- My Custom Code { -->
<tr>
<th><label for="custom_field_1">Custom Field Label</label></th>
<td><input type="text" name="custom_field_1" id="custom_field_1" /></td>
</tr>
<!-- } -->
</table>
<script>
jQuery(function($){
//Move my HTML code below user\'s role
$(\'#table_custom_field_1 tr\').insertBefore($(\'#role\').parentsUntil(\'tr\').parent());
});
</script>
<?php
}
add_action(\'admin_footer_text\', \'wpse_135622_add_new_user_custom_field_hack\');
当然,重命名为
custom_field_1
你要做一些对你有意义的事情。例如
pets_name
或者别的什么。
步骤2:现在该字段显示在注册表上,我们需要在单击“添加新用户”时将其保存到数据库中:
// Save custom field
function wpse_135622_save_custom_add_new_user_field_hack($user_id){
# do this only if you can
if(!current_user_can(\'manage_options\'))
return false;
# save my custom field
update_usermeta($user_id, \'user_custom_field_1\', $_POST[\'custom_field_1\']);
}
add_action(\'user_register\', \'wpse_135622_save_custom_add_new_user_field_hack\');
再次,重命名每个
custom_field_1
来匹配你上面所做的。
现在它已保存,以下是您可以在管理仪表板>用户中查看它的方式:
// Add custom fields to user profile page.
function wpse135622_show_custom_user_fields($profile_fields) {
$profile_fields[\'user_custom_field_1\'] = \'Custom Field 1\';
return $profile_fields;
}
add_filter(\'user_contactmethods\', \'wpse135622_show_custom_user_fields\');
再次匹配
custom_field_1
您一直使用的名称
Custom Field 1
是要在“管理用户”页面上显示的标签。
这应该是总结,让你开始。因为它现在在数据库中,所以您可以对它做任何您想做的事情-例如,如果用户登录,则将其显示在首页上。
如果这回答了您的问题,请单击复选标记将其标记为已完成。