最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to insert new values to Wordpress user Firstname and Surname Fields via DB

programmeradmin1浏览0评论

I have a custom sign up form in my Wordpress site which has confirmation email functionality for activating the account. For the confirmation step am keeping another DB table where am keeping the pending users info and when they are going with confirmation link then am copying the info from that table and adding to wp_users table. The issue is that i have First Name and Surname fields which info is being kept in another Wp table called wp_usermeta.

So my question is how i can insert the corresponding user firstname, surname when am adding the user to wp_users after confirmation like this

$checkUserID = $wpdb->get_results("SELECT * FROM pendingwpusers WHERE token = '".$gettokenval."'");
          $checkUserIDMain = $wpdb->query("SELECT * FROM store_users WHERE TrackNumber = '".$gettokenval."'"); 
          //$aaaa = mysql_num_rows($checkUserIDMain);
         //var_dump($checkUserIDMain);
            if($checkUserID && $checkUserIDMain == 0){    
                foreach ($checkUserID as $checkUser) {
                    //if(wp_mail($to, $subject, $message, $header)){}else{mail($to, $subject, $message, $header);}
                    $hashedpass = md5($checkUser->user_pass);
                    $wpdb->insert(
                        'store_users',
                        array(
                            'user_login' => $checkUser->user_login,
                            'user_pass' => $hashedpass,
                            'user_nicename' => $checkUser->user_nicename,
                            'user_email' => $checkUser->user_email,
                            'user_registered' => $checkUser->user_registered,
                            'display_name' => $checkUser->display_name,
                            'TrackNumber' => $checkUser->token
                        ),
                        array(
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s'
                        )
                    ); 
                }
          }

I have a custom sign up form in my Wordpress site which has confirmation email functionality for activating the account. For the confirmation step am keeping another DB table where am keeping the pending users info and when they are going with confirmation link then am copying the info from that table and adding to wp_users table. The issue is that i have First Name and Surname fields which info is being kept in another Wp table called wp_usermeta.

So my question is how i can insert the corresponding user firstname, surname when am adding the user to wp_users after confirmation like this

$checkUserID = $wpdb->get_results("SELECT * FROM pendingwpusers WHERE token = '".$gettokenval."'");
          $checkUserIDMain = $wpdb->query("SELECT * FROM store_users WHERE TrackNumber = '".$gettokenval."'"); 
          //$aaaa = mysql_num_rows($checkUserIDMain);
         //var_dump($checkUserIDMain);
            if($checkUserID && $checkUserIDMain == 0){    
                foreach ($checkUserID as $checkUser) {
                    //if(wp_mail($to, $subject, $message, $header)){}else{mail($to, $subject, $message, $header);}
                    $hashedpass = md5($checkUser->user_pass);
                    $wpdb->insert(
                        'store_users',
                        array(
                            'user_login' => $checkUser->user_login,
                            'user_pass' => $hashedpass,
                            'user_nicename' => $checkUser->user_nicename,
                            'user_email' => $checkUser->user_email,
                            'user_registered' => $checkUser->user_registered,
                            'display_name' => $checkUser->display_name,
                            'TrackNumber' => $checkUser->token
                        ),
                        array(
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s'
                        )
                    ); 
                }
          }
Share Improve this question asked May 3, 2016 at 12:37 Ana DEVAna DEV 1751 gold badge1 silver badge10 bronze badges 1
  • Any progress on the question/answer? – kaiser Commented May 20, 2016 at 11:14
Add a comment  | 

2 Answers 2

Reset to default 15

If you have the ID of the user you can do this:

wp_update_user([
    'ID' => $userId, // this is the ID of the user you want to update.
    'first_name' => $firstName,
    'last_name' => $lastName,
]);

You can update / insert almost all fields with this function. Take a look at the documentation here

I had a similar issue (woocommerce test users) and wrote this shell script:

#!/bin/bash

# set for your local setup
DB=wordpress ; DBUSER=wordpress ; DBPASS=wordpress

INS_WPU="INSERT INTO wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name) VALUES"
INS_WPM="INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES"
# password is 'testuser'
TESTUSERPASS='$P$BptzXm87Y8pxffjyy4Ur0ANs8uqW7J.'

function ins_test_user(){
    # 2 parameters : firstname, lastname
    NAM1="$1"
    NAM2="$2"

    # get the last unused wp_users ID
    SQL="select 1+max(ID) from wp_users into @wpuid;\n"
    # get the last unused wp_usermeta ID
    SQL="${SQL}select 1+max(umeta_id) from wp_usermeta into @wpmid;\n"

    # insert wp_users record
    SQL="${SQL}${INS_WPU} (@wpuid, '${NAM1}${NAM2}','${TESTUSERPASS}','${NAM1} ${NAM2}','${NAM1}.${NAM2}@example','http://${NAM1}.${NAM2}.example',now(),'',0,'test user ${CODE}');\n"

    # insert wp_usermeta records
    SQL="${SQL}${INS_WPM}(0+@wpmid,@wpuid,'billing_phone','');\n"
    SQL="${SQL}${INS_WPM}(1+@wpmid,@wpuid,'nickname','${NAM1} ${NAM2}');\n"
    SQL="${SQL}${INS_WPM}(2+@wpmid,@wpuid,'first_name','${NAM1}');\n"
    SQL="${SQL}${INS_WPM}(3+@wpmid,@wpuid,'last_name','${NAM2}');\n"
    SQL="${SQL}${INS_WPM}(4+@wpmid,@wpuid,'description','');\n"
    SQL="${SQL}${INS_WPM}(5+@wpmid,@wpuid,'rich_editing','true');\n"
    SQL="${SQL}${INS_WPM}(6+@wpmid,@wpuid,'syntax_highlighting','true');\n"
    SQL="${SQL}${INS_WPM}(7+@wpmid,@wpuid,'comment_shortcuts','false');\n"
    SQL="${SQL}${INS_WPM}(8+@wpmid,@wpuid,'admin_color','fresh');\n"
    SQL="${SQL}${INS_WPM}(9+@wpmid,@wpuid,'use_ssl','0');\n"
    SQL="${SQL}${INS_WPM}(10+@wpmid,@wpuid,'show_admin_bar_front','true');\n"
    SQL="${SQL}${INS_WPM}(11+@wpmid,@wpuid,'locale','');\n"
    SQL="${SQL}${INS_WPM}(12+@wpmid,@wpuid,'wp_capabilities','a:1:{s:8:\"customer\";b:1;}');\n"
    SQL="${SQL}${INS_WPM}(13+@wpmid,@wpuid,'wp_user_level','0');\n"
    SQL="${SQL}${INS_WPM}(14+@wpmid,@wpuid,'dismissed_wp_pointers','');\n"
    SQL="${SQL}${INS_WPM}(15+@wpmid,@wpuid,'billing_first_name','${NAM1}');\n"
    SQL="${SQL}${INS_WPM}(16+@wpmid,@wpuid,'billing_last_name','${NAM2}');\n"
    SQL="${SQL}${INS_WPM}(17+@wpmid,@wpuid,'billing_company','');\n"
    SQL="${SQL}${INS_WPM}(18+@wpmid,@wpuid,'billing_address_1','somewhere');\n"
    SQL="${SQL}${INS_WPM}(19+@wpmid,@wpuid,'billing_address_2','la la land');\n"
    SQL="${SQL}${INS_WPM}(20+@wpmid,@wpuid,'billing_city','X');\n"
    SQL="${SQL}${INS_WPM}(21+@wpmid,@wpuid,'billing_postcode','90210');\n"
    SQL="${SQL}${INS_WPM}(22+@wpmid,@wpuid,'billing_country','AF');\n"
    SQL="${SQL}${INS_WPM}(23+@wpmid,@wpuid,'billing_state','');\n"
    SQL="${SQL}${INS_WPM}(24+@wpmid,@wpuid,'billing_email','${NAM1}.${NAM2}@example');\n"
    SQL="${SQL}${INS_WPM}(25+@wpmid,@wpuid,'shipping_first_name','');\n"
    SQL="${SQL}${INS_WPM}(26+@wpmid,@wpuid,'shipping_last_name','');\n"
    SQL="${SQL}${INS_WPM}(27+@wpmid,@wpuid,'shipping_company','');\n"
    SQL="${SQL}${INS_WPM}(28+@wpmid,@wpuid,'shipping_address_1','');\n"
    SQL="${SQL}${INS_WPM}(29+@wpmid,@wpuid,'shipping_address_2','');\n"
    SQL="${SQL}${INS_WPM}(30+@wpmid,@wpuid,'shipping_city','');\n"
    SQL="${SQL}${INS_WPM}(31+@wpmid,@wpuid,'shipping_postcode','');\n"
    SQL="${SQL}${INS_WPM}(32+@wpmid,@wpuid,'shipping_country','');\n"
    SQL="${SQL}${INS_WPM}(33+@wpmid,@wpuid,'shipping_state','');\n"

    # commit after each user
    SQL="${SQL}commit;\n"

    echo -e "$SQL"
    }

{
echo "set autocommit=off;\n"

ins_test_user jane doe
ins_test_user joe bloggs
ins_test_user architeuthis dux
ins_test_user ftagn fnord
ins_test_user pete brick
ins_test_user kaiser soze

echo "set autocommit=on;\n"
} | mysql -D $DB -u $DBUSER --password="$DBPASS"
发布评论

评论列表(0)

  1. 暂无评论