如何通过数据库向WordPress用户名字和姓氏字段插入新值

时间:2016-05-03 作者:Anahit DEV

我在Wordpress网站上有一个自定义的注册表单,它具有激活帐户的确认电子邮件功能。对于确认步骤,我保留另一个DB表,其中我保留挂起的用户信息,当他们使用确认链接时,我将从该表复制信息并添加到wp\\U users表。问题是我有名字和姓氏字段,这些信息保存在另一个名为Wp\\u usermeta的Wp表中。

因此,我的问题是,在像这样确认后将用户添加到wp\\U用户时,如何插入相应的用户名、姓氏

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

2 个回复
最合适的回答,由SO网友:Refilon 整理而成

如果您有该用户的ID,则可以执行以下操作:

wp_update_user([
    \'ID\' => $userId, // this is the ID of the user you want to update.
    \'first_name\' => $firstName,
    \'last_name\' => $lastName,
]);
您可以使用此函数更新/插入几乎所有字段。查看文档here

SO网友:jmullee

我也有类似的问题(woocommerce测试用户),并编写了以下shell脚本:

#!/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.org\',\'http://${NAM1}.${NAM2}.example.com\',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.org\');\\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"

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="