添加额外字段 - Wordpress(wp_insert_user& update_user_meta不起作用)

时间:2014-11-11 14:29:22

标签: php wordpress forms

我正在使用wordpress网站并使用插件:zm-ajax-login-register来注册&登录。

使用我正在使用的代码,它仍然只注册登录名,电子邮件和密码。

我的额外字段未添加到数据库中。

您可以在此处找到所有代码:Github

我在寄存器表单中创建了额外的字段,如下所示:

<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>

在注册页面中,我已在用户数组中添加了我的数据:

$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'] )
        );

然后使用wp_insert_user我插入我的数据:

$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 );

之后我想使用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'] );

wp_insert_user既不会添加first-name也不会last-name,也不会update_user_meta向数据库添加任何内容。

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

如果您尝试添加新字段,则需要使用add_user_meta不更新,因为数据库中没有记录将其更新为类似

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

如果您有任何问题,建议您查看wordpress codex

<强> 修改

@Alex做以下事情应该做你需要的事情

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 );
}