向“Add New User”添加字段,但表单数据未保存到数据库

时间:2014-12-10 作者:need-help

i have add new fields when we create new user in user-new.php

the new field appears in the form, but the form data not saved into DB.

after i created new user, i go to "edit user" and see the field is empty.

any sugassins to make this work?

here is the code:

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\');

UpdateThanks to @cybmeta, the code working great in single website but not in the multisite.

i think the problem is that the user not created right away. the user created after the user click on "activation link" in the email his get from wordpress.

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

我在您的代码中发现以下错误:

自定义用户字段的保存过程与注册挂钩,但与更新用户配置文件操作无关您正在使用update_usermeta, 过时的函数,使用update_user_meta 取而代之的是您正在使用get_the_author_meta 具有$user->ID 未在if之前检查$user 是一个对象,它在新用户注册表上生成错误(我也建议使用get_user_meta 而是)您没有清理/验证公司字段的数据(我不确定您希望这里提供什么类型的数据saniteze_text_field 例如)

function custom_user_profile_fields($user){
    $previous_value = \'\';
    if( is_object($user) && isset($user->ID) ) {
        $previous_value = get_user_meta( $user->ID, \'company\', true );
    }
    ?>
    <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( $previous_value ); ?>" 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){

    if(!current_user_can(\'manage_options\'))
        return false;

    # save my custom field
    if( isset($_POST[\'company\']) ) {
        update_user_meta( $user_id, \'company\', sanitize_text_field( $_POST[\'company\'] ) );
    } else {
        //Delete the company field if $_POST[\'company\'] is not set
        delete_user_meta( $user_id, \'company\', $meta_value );
    }
}
add_action(\'user_register\', \'save_custom_user_profile_fields\');
add_action( \'personal_options_update\', \'save_custom_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'save_custom_user_profile_fields\' );
此外,如果任何用户没有“manage\\u options”功能,这段代码会阻止其保存数据,这是通常只有管理员才具有的功能,因此用户无法更新这些字段:

if(!current_user_can(\'manage_options\'))
    return false;
因此,如果需要,请删除它或再次检查用户能力。例如,检查当前用户是否可以编辑正在更新的用户似乎更好:

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

结束

相关推荐

Sort users by meta_value_num

我在使用WP_User_Query 要按元数值对用户进行排序,我认为这很简单,因为它只按字母降序显示结果。 <?php $args = array( \'orderby\' => \'meta_value_num\', \'meta_key \' => \'epicredvote\', \'order\' => \'DESC\', ); // The Query $user_query = n