0

I'm trying to modify a login plugin to make new users to to enter the display name to show on the site when they register. I know that the display name is located in the wp-users table of the database, but cant seam to be able to write to it.


Edit

<?php

function wps_pro_login_registration_errors( $errors ) {
    if ( empty( $_POST['first_name'] ) )
        $errors->add( 'empty_first_name', '<strong>ERROR</strong>: Please enter your first name.' );
    if ( empty( $_POST['last_name'] ) )
        $errors->add( 'empty_last_name', '<strong>ERROR</strong>: Please enter your last name.' );
    if ( empty( $_POST['nickname'] ) )
        $errors->add( 'empty_nickname', '<strong>ERROR</strong>: Please enter your display name.' );
    return $errors;
}
add_filter( 'registration_errors', 'wps_pro_login_registration_errors' );
function wps_pro_login_user_register( $user_id )
{
    if (!empty($_POST['first_name']))
        update_user_meta($user_id, 'first_name', $_POST['first_name']);
    if (!empty($_POST['last_name']))
        update_user_meta($user_id, 'last_name', $_POST['last_name']);
    if ( !empty( $_POST['nickname'] ) )
        update_user_meta( $user_id, 'nickname', $_POST['nickname'] );
}
add_action( 'user_register', 'wps_pro_login_user_register' );

?>

This works fine to write to wp_usermeta table but since the display name is in the wp_users table it does not work.

I've added the following with no success.

if ( empty( $_POST['displayname'] ) )
        $errors->add( 'empty_displayname', '<strong>ERROR</strong>: Please enter your display name.' );

in the upper section and the following in the lower section

if ( !empty( $_POST['displayname'] ) )
        update_user_meta( $user_id, 'displayname', $_POST['displayname'] );
0

You need to use wp_update_user function to update user info. Please see below code:

<?php

$user_id = 1; /* It would be dynamic in your case */

$display_name = $_POST['display_name'];

$user_id = wp_update_user( array( 'ID' => $user_id, 'display_name' => $display_name ) );

if ( is_wp_error( $user_id ) ) {
    // There was an error, probably that user doesn't exist.
} else {
    // Success!
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.