Grow your CSS skills. Land your dream job.

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

  • # September 29, 2011 at 9:11 pm

    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.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".