向仪表板中的“Add New User”屏幕添加字段

时间:2011-07-22 作者:Zach Shallbetter

我想在“管理”面板的“添加新用户”页面中添加“公司名称”字段。我已经做了相当多的搜索,但无法找到有关如何执行此操作的详细信息。我可以轻松地将信息添加到个人资料页面并注册。。

   function my_custom_userfields( $contactmethods ) {
    //Adds customer contact details
    $contactmethods[\'company_name\'] = \'Company Name\';
    return $contactmethods;
   }
   add_filter(\'user_contactmethods\',\'my_custom_userfields\',10,1);
但不要在其他任何事情上冒险。

9 个回复
最合适的回答,由SO网友:Zach Shallbetter 整理而成

为了做到这一点,您必须手动更改新用户。php页面。这不是正确的处理方法,但如果你非常需要,这就是处理方法。

我补充道

<tr class="form-field">
    <th scope="row"><label for="company_name"><?php _e(\'Company Name\') ?> </label></th>
    <td><input name="company_name" type="text" id="company_name" value="<?php echo esc_attr($new_user_companyname); ?>" /></td>
</tr>
我还将信息添加到函数中。php

   function my_custom_userfields( $contactmethods ) {
    $contactmethods[\'company_name\']             = \'Company Name\';
    return $contactmethods;
   }
   add_filter(\'user_contactmethods\',\'my_custom_userfields\',10,1);

SO网友:Tahir Yasin

user_new_form 是可以在这里变魔术的钩子。

function custom_user_profile_fields($user){
  ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( get_the_author_meta( \'company\', $user->ID ) ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
  <?php
}
add_action( \'show_user_profile\', \'custom_user_profile_fields\' );
add_action( \'edit_user_profile\', \'custom_user_profile_fields\' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can(\'manage_options\'))
        return false;

    # save my custom field
    update_usermeta($user_id, \'company\', $_POST[\'company\']);
}
add_action(\'user_register\', \'save_custom_user_profile_fields\');
add_action(\'profile_update\', \'save_custom_user_profile_fields\');
有关详细信息,请访问我的博客帖子:http://scriptbaker.com/adding-custom-fields-to-wordpress-user-profile-and-add-new-user-page/

SO网友:NkR

我也有同样的需求,创建了以下黑客:

<?php
function hack_add_custom_user_profile_fields(){
    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_my_custom_field" style="display:none;">
<!-- My Custom Code { -->
    <tr>
        <th><label for="my_custom_field">My Custom Field</label></th>
        <td><input type="text" name="my_custom_field" id="my_custom_field" /></td>
    </tr>
<!-- } -->
</table>
<script>
jQuery(function($){
    //Move my HTML code below user\'s role
    $(\'#table_my_custom_field tr\').insertAfter($(\'#role\').parentsUntil(\'tr\').parent());
});
</script>
<?php
}
add_action(\'admin_footer_text\', \'hack_add_custom_user_profile_fields\');


function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can(\'manage_options\'))
        return false;

    # save my custom field
    update_usermeta($user_id, \'my_custom_field\', $_POST[\'my_custom_field\']);
}
add_action(\'user_register\', \'save_custom_user_profile_fields\');

SO网友:maheshwaghmare

你需要做两件事。

注册字段保存字段

Note: Below example works only for administrator user role.


1。注册字段Add New User 使用操作user_new_form

对于User Profile 使用操作show_user_profile, edit_user_profile

Register fields Snippet:

/**
 * Add fields to user profile screen, add new user screen
 */
if( !function_exists(\'m_register_profile_fields\') ) {

    //  This action for \'Add New User\' screen
    add_action( \'user_new_form\', \'m_register_profile_fields\' );

    //  This actions for \'User Profile\' screen
    add_action( \'show_user_profile\', \'m_register_profile_fields\' );
    add_action( \'edit_user_profile\', \'m_register_profile_fields\' );

    function m_register_profile_fields( $user ) {

        if ( !current_user_can( \'administrator\', $user_id ) )
            return false;

        ?>

        <h3>Client Portal</h3>
        <table class="form-table">
            <tr>
                <th><label for="dropdown">Portal Category</label></th>
                <td>
                    <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( \'portal_cat\', $user->ID ) ); ?>" id="portal_cat" /><br />
                </td>
            </tr>
        </table>
    <?php }
}
2。保存字段Add New User 使用操作user_register

对于User Profile 使用操作personal_options_update, edit_user_profile_update

Save fields Snippet:

/**
 *  Save portal category field to user profile page, add new profile page etc
 */
if( !function_exists(\'m_register_profile_fields\') ) {

    //  This action for \'Add New User\' screen
    add_action( \'user_register\', \'cp_save_profile_fields\' );

    //  This actions for \'User Profile\' screen
    add_action( \'personal_options_update\', \'cp_save_profile_fields\' );
    add_action( \'edit_user_profile_update\', \'cp_save_profile_fields\' );

    function cp_save_profile_fields( $user_id ) {

        if ( !current_user_can( \'administrator\', $user_id ) )
            return false;

        update_usermeta( $user_id, \'portal_cat\', $_POST[\'portal_cat\'] );
    }
}
完整的代码段:

/**
 * Add fields to user profile screen, add new user screen
 */
if( !function_exists(\'m_register_profile_fields\') ) {

    //  This action for \'Add New User\' screen
    add_action( \'user_new_form\', \'m_register_profile_fields\' );

    //  This actions for \'User Profile\' screen
    add_action( \'show_user_profile\', \'m_register_profile_fields\' );
    add_action( \'edit_user_profile\', \'m_register_profile_fields\' );

    function m_register_profile_fields( $user ) {

        if ( !current_user_can( \'administrator\', $user_id ) )
            return false;

        ?>

        <h3>Client Portal</h3>
        <table class="form-table">
            <tr>
                <th><label for="dropdown">Portal Category</label></th>
                <td>
                    <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( \'portal_cat\', $user->ID ) ); ?>" id="portal_cat" /><br />
                </td>
            </tr>
        </table>
    <?php }
}


/**
 *  Save portal category field to user profile page, add new profile page etc
 */
if( !function_exists(\'m_register_profile_fields\') ) {

    //  This action for \'Add New User\' screen
    add_action( \'user_register\', \'cp_save_profile_fields\' );

    //  This actions for \'User Profile\' screen
    add_action( \'personal_options_update\', \'cp_save_profile_fields\' );
    add_action( \'edit_user_profile_update\', \'cp_save_profile_fields\' );

    function cp_save_profile_fields( $user_id ) {

        if ( !current_user_can( \'administrator\', $user_id ) )
            return false;

        update_usermeta( $user_id, \'portal_cat\', $_POST[\'portal_cat\'] );
    }
}

SO网友:ojrask

我可以使用user_new_form_tag 位于user-new.php 页面的表单开始标记。最后,如果在输出HTML之后,您只需要开始输出> 并删除最后输出的> 您自己的代码。如下所示:

function add_new_field_to_useradd()
{
    echo "><div>"; // Note the first \'>\' here. We wrap our own output to a \'div\' element.

    // Your wanted output code should be here here.

    echo "</div"; // Note the missing \'>\' here.
}

add_action( "user_new_form_tag", "add_new_field_to_useradd" );
Theuser_new_form_tag 位于user-new.php 303线周围(至少在WP3.5.1中):

...
<p><?php _e(\'Create a brand new user and add it to this site.\'); ?></p>
<form action="" method="post" name="createuser" id="createuser" class="validate"<?php do_action(\'user_new_form_tag\');?>>
<input name="action" type="hidden" value="createuser" />
...
当然,这里的缺点是,所有自定义字段必须首先出现在表单中,在WP core中声明的字段之前。

SO网友:Mayeenul Islam

无论我们如何对函数中的表单字段进行排序,挂钩都很重要。遵循我的内联注释。截至WordPress 4.2.2,我们现在有很多挂钩:

<?php
/**
 * Declaring the form fields
 */
function show_my_fields( $user ) {
   $fetched_field = get_user_meta( $user->ID, \'my_field\', true ); ?>
    <tr class="form-field">
       <th scope="row"><label for="my-field"><?php _e(\'Field Name\') ?> </label></th>
       <td><input name="my_field" type="text" id="my-field" value="<?php echo esc_attr($fetched_field); ?>" /></td>
    </tr>
<?php
}
add_action( \'show_user_profile\', \'show_my_fields\' ); //show in my profile.php page
add_action( \'edit_user_profile\', \'show_my_fields\' ); //show in my profile.php page

//add_action( \'user_new_form_tag\', \'show_my_fields\' ); //to add the fields before the user-new.php form
add_action( \'user_new_form\', \'show_my_fields\' ); //to add the fields after the user-new.php form

/**
 * Saving my form fields
 */
function save_my_form_fields( $user_id ) {
    update_user_meta( $user_id, \'my_field\', $_POST[\'my_field\'] );
}
add_action( \'personal_options_update\', \'save_my_form_fields\' ); //for profile page update
add_action( \'edit_user_profile_update\', \'save_my_form_fields\' ); //for profile page update

add_action( \'user_register\', \'save_my_form_fields\' ); //for user-new.php page new user addition

SO网友:Bainternet

user_contactmethods 筛选器挂钩在user-new.php 页面这样做行不通,如果你look at the source 您将看到,没有可以用于向添加新用户表单添加额外字段的挂钩。

因此,这只能通过修改核心文件(BIG NO NO)或使用JavaScript或jQuery添加字段并捕获字段来实现。

或者您可以创建Ticket at the Trac

SO网友:Linish

以下代码将在“添加用户”窗体中显示“个人信息”


function display_bio_field() {
  echo "The field html";
}
add_action(\'user_new_form\', \'display_bio_field\');

SO网友:AndrettiMilas

对于“添加新用户”页面,这是不可行的,但如果您想在“您的配置文件”页面(用户可以在其中编辑其配置文件)中实现这一点,则可以在函数中尝试。php:

add_action( \'show_user_profile\', \'my_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'my_show_extra_profile_fields\' );
function my_show_extra_profile_fields( $user ) { ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="companyname">Company Name</label></th>
            <td>
                <input type="text" name="companyname" id="companyname" value="<?php echo esc_attr( get_the_author_meta( \'companyname\', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php }

结束

相关推荐

Functions file mods and CPU

将添加类似于我在下面粘贴到函数的代码。php主题文件会降低Wordpress站点的速度,还是影响CPU?(谢谢)function remove_menu_items() { global $menu; $restricted = array(__(\'Links\'), __(\'Comments\'), __(\'Media\'), __(\'Plugins\'), __(\'Tools\'), __(\'Users\')); end ($menu);&#