注册和配置文件自定义域

时间:2014-02-23 作者:Plaz

如何在wordpress注册表中添加自定义字段并在后端的用户配置文件中查看?

我已经在谷歌上搜索过了,但只是想知道如何将其添加到用户档案中,而不是注册过程中。

2 个回复
SO网友:Pat

我只是自己经历了整个过程。没有一个钩子或任何东西可以让这变得容易,因此我们必须将其“破解”到位,以使其工作。这并不漂亮,但它完成了任务。我已经在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 是要在“管理用户”页面上显示的标签。

这应该是总结,让你开始。因为它现在在数据库中,所以您可以对它做任何您想做的事情-例如,如果用户登录,则将其显示在首页上。

如果这回答了您的问题,请单击复选标记将其标记为已完成。

SO网友:s_ha_dum

如果“wordpress注册表”是指wp-login.php?action=register 然后there is a hook 可用于在“电子邮件”行下方添加字段。例如

add_action(
  \'register_form\',
  function() {
    echo \'Here is where the hook fires\';
  }
);
要保存数据,请连接到user_register as in this example from the Codex:

add_action( \'user_register\', \'myplugin_registration_save\', 10, 1 );

function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST[\'first_name\'] ) )
        update_user_meta($user_id, \'first_name\', $_POST[\'first_name\']);

}
你的问题没有什么细节,所以很难更具体。

结束

相关推荐

Too many actions/filters!

这里是wordpress的新成员。动作/过滤器的概念本身并不难理解。令我不知所措的是大量可用的操作和过滤器。当我阅读教程/指南时,他们会说“只需将此功能添加到wp\\U head操作或after\\U setup\\u主题”。如果没有这些教程,我究竟如何知道将该函数与该操作挂钩?作为一个初学者,我怎么会知道什么是合适的操作?有没有关于如何导航的建议?谢谢