I’m using the following:
function file_custom_fields( $user_id ) {
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_first', $_POST );
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_last', $_POST );
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_company', $_POST );
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_address', $_POST );
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_phone', $_POST );
if ( isset( $_POST ) )
update_user_meta( $user_id, 'user_fax', $_POST );
}
add_action( 'user_register', 'file_custom_fields' );
I was wondering if there’s a more efficient way of doing it? Is something like the following good?
function file_custom_fields( $user_id ) {
$fields = array (
'user_first' => 'user_first',
'user_last' => 'user_last',
'user_company' => 'user_company',
'user_address' => 'user_address',
'user_phone' => 'user_phone',
'user_fax' => 'user_fax');
foreach ($fields as $key => $value ) {
if ( isset( $_POST[$value] ) ) {
update_user_meta( $user_id, $key, $_POST[$value] );
}
}
}
add_action( 'user_register', 'file_custom_fields' );
I know the key and value are the same, but I did it that way just in case they need to be different. The key is what is used as the field name into the database. The value is what is used as the field name from the form.