treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Is this a good way of filing a number of values?

  • I'm using the following:

    function file_custom_fields( $user_id ) {
    if ( isset( $_POST['user_first'] ) )
    update_user_meta( $user_id, 'user_first', $_POST['user_first'] );
    if ( isset( $_POST['user_last'] ) )
    update_user_meta( $user_id, 'user_last', $_POST['user_last'] );
    if ( isset( $_POST['user_company'] ) )
    update_user_meta( $user_id, 'user_company', $_POST['user_company'] );
    if ( isset( $_POST['user_address'] ) )
    update_user_meta( $user_id, 'user_address', $_POST['user_address'] );
    if ( isset( $_POST['user_phone'] ) )
    update_user_meta( $user_id, 'user_phone', $_POST['user_phone'] );
    if ( isset( $_POST['user_fax'] ) )
    update_user_meta( $user_id, 'user_fax', $_POST['user_fax'] );
    }
    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.