Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Created October 27, 2012 04:19
Show Gist options
  • Save DrewAPicture/3962923 to your computer and use it in GitHub Desktop.
Save DrewAPicture/3962923 to your computer and use it in GitHub Desktop.
Make 'First Last' default-style display name
/**
* Build 'First Last' user display name
*
* Sets up default-style Display Name for users on new registrations
*
* @param int $user_id
* @uses wp_insert_user()
*/
function ww_default_display_name( $user_id ) {
$first = get_user_meta( $user_id, 'first_name', true );
$last = get_user_meta( $user_id, 'last_name', true );
// Let's get together now
$display = $first . " " . $last;
// Update user
wp_update_user( array( "ID" = $user_id, "display_name" = $display ) );
}
add_action( 'user_register', 'ww_default_display_name' );
/**
* Filter display name before saving
*
* @param string $name
* @uses sanitize_text_field()
* @return string $output
*/
function ww_filter_default_display_name( $name ) {
if ( isset( $_POST['display_name'] ) )
return sanitize_text_field( $_POST['display_name'] );
if ( isset( $_POST['first_name'] ) ) {
$output = sanitize_text_field( $_POST['first_name'] );
if ( isset( $_POST['last_name'] ) )
$output .= ' '. sanitize_text_field( $_POST['last_name'] );
}
return $output;
}
add_filter( 'pre_user_display_name', 'ww_filter_default_display_name' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment