Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding extra fields - Wordpress (wp_insert_user & update_user_meta doesn't work)

I am working on a wordpress site and using a plugin: zm-ajax-login-register to register & login.

With the code I am using, it still only registers the login-name, the email and the password.

My extra fields aren't added to the database.

All the code you can find here at: Github

I have created extra fields in the register form like this:

<div class="form-wrapper">
                    <?php
                    wp_nonce_field( 'facebook-nonce', 'facebook_security' );
                    wp_nonce_field( 'register_submit', 'security' );
                    ?>
                    <div class="ajax-login-register-status-container">
                        <div class="ajax-login-register-msg-target"></div>
                    </div>
                    <div class="noon"><label><?php _e('User Name', 'ajax_login_register'); ?></label><input type="text" required name="login" class="user_login" /></div>

                    <div class="noon"><label><?php _e('First Name', 'ajax_login_register'); ?></label><input type="text" required name="first_name" class="first_name" /></div>
                    <div class="noon"><label><?php _e('Last Name', 'ajax_login_register'); ?></label><input type="text" required name="last_name" class="last_name" /></div>

                    <div class="noon"><label><?php _e('Email', 'ajax_login_register'); ?></label><input type="text" required name="email" class="user_email ajax-login-register-validate-email" /></div>

                    <div class="noon"><label><?php _e('Street', 'ajax_login_register'); ?></label><input type="text" required name="street" class="street" /></div>
                    <div class="noon"><label><?php _e('Housenumber', 'ajax_login_register'); ?></label><input type="number" required name="housenumber" class="housenumber" /></div>
                    <div class="noon"><label><?php _e('City', 'ajax_login_register'); ?></label><input type="text" required name="city" class="city" /></div>
                    <div class="noon"><label><?php _e('Zipcode', 'ajax_login_register'); ?></label><input type="number" required name="zipcode" class="zipcode" /></div>
                    <div class="noon"><label><?php _e('Phone', 'ajax_login_register'); ?></label><input type="number" required name="phone" class="phone" /></div>

                    <?php do_action( 'zm_ajax_login_register_below_email_field' ); ?>

                    <div class="noon"><label><?php _e('Password', 'ajax_login_register'); ?></label><input type="password" required name="password" class="user_password" /></div>
                    <div class="noon"><label><?php _e('Confirm Password', 'ajax_login_register'); ?></label><input type="password" required name="confirm_password" class="user_confirm_password" /></div>

                    <div class="noon"><a href="#" class="already-registered-handle"><?php echo apply_filters( 'ajax_login_register_already_registered_text', __('Already registered?','ajax_login_register') ); ?></a></div>
                    <div class="button-container">
                        <input class="register_button green" type="submit" value="<?php _e('Register','ajax_login_register'); ?>" accesskey="p" name="register" disabled />
                    </div>
                </div>

In the register page i have added my data in the user array:

$user = array(
            'login'    => empty( $_POST['login'] ) ? $login : sanitize_text_field( $_POST['login'] ),

            'first_name'    => empty($_POST['first_name']),
            'last_name'    => empty($_POST['last_name']),

            'email'    => empty( $_POST['email'] ) ? $email : sanitize_text_field( $_POST['email'] ),

            'street'    => empty( $_POST['street'] ),
            'housenumber'    => empty( $_POST['housenumber'] ),
            'city'    => empty( $_POST['city'] ),
            'zipcode'    => empty( $_POST['zipcode'] ),
            'phone'    => empty( $_POST['phone'] ),

            'password' => empty( $_POST['password'] ) ? $password : sanitize_text_field( $_POST['password'] ),
            'fb_id'    => empty( $_POST['fb_id'] ) ? false : sanitize_text_field( $_POST['fb_id'] )
        );

Then using wp_insert_user I insert my data like this:

$userdata = array(
                        'user_login' => $user['login'],
                        'user_pass' => $user['password'],
                        'first_name' => $user['first_name'],
                        'last_name' => $user['last_name'],
                        'user_nicename' => $user['login'],
                        'user_email' => $user['email'],
                        'user_registered' => date_default_timezone_get(),
                        'display_name' => $user['login'],
                        /*'street' => $user['street'],
                        'housenumber' => $user['housenumber'],
                        'city' => $user['city'],
                        'zipcode' => $user['zipcode'],
                        'phone' => $user['phone'],*/
                    );

            $user_id = wp_insert_user( $userdata );

After that I want to add some extra fields to my database using update_user_meta

update_user_meta( $user_id, 'street', $user['street'] );
update_user_meta( $user_id, 'housenumber', $user['housenumber'] );
update_user_meta( $user_id, 'city', $user['city'] );
update_user_meta( $user_id, 'zipcode', $user['zipcode'] );
update_user_meta( $user_id, 'phone', $user['phone'] );

Neither the wp_insert_user adds first-name or last-name nor does the update_user_meta add anything to the database.

Where did I go wrong?

like image 980
Sidney Van Peteghem Avatar asked Nov 11 '14 14:11

Sidney Van Peteghem


1 Answers

If you are trying to add new fields you need to use add_user_meta not update as there is no record within the database to update it to like so

<?php add_user_meta( "user_id", "shoe_size", "13", false ); ?>

if you have any issues id suggest viewing the wordpress codex

Edit

@Alex do the following it should do what you need

if (get_user_meta($userID, 'street', true)){
      update_user_meta( $user_id, 'street', $_POST['street'], true );
}else{
      add_user_meta( $user_id, 'street', $_POST['street'], false );
}
like image 76
Dava Gordon Avatar answered Oct 18 '22 16:10

Dava Gordon