WordPress creating users in MySQL via script wp_user_level wp_capabilities wp_users

Scenario:
Creating a WordPress blog with an entire company of users.
We want employees to already have an account, so that registration may be turned OFF otherwise.
From an excel list of employees, we’ll extract their email, make an account for each email in the WordPress database, and then they may change their passwords themselves. You can imagine what the php script will look like, to extract the email, and foreach insert the following (uniqueusername and 5 replaced of course with incrementing id numbers and changing emailnames@ourcompany.com):

 

INSERT INTO wp_users
(id,user_login,user_pass,user_email,display_name)
VALUES
(‘5′,’uniqueusername’,md5(‘pass’),’uniqueusername@ourcompany.com’,’uniqueusername’);

 

// to note, this below is simultaneously a nice example of how to insert multiple rows with one MySQL INSERT statement

 

INSERT INTO wp_usermeta
(user_id,meta_key,meta_value)
VALUES
(‘5′,’wp_capabilities’,’a:1:{s:6:”author”;s:1:”1″;}’),
(‘5′,’wp_user_level’,’10’);

 

Thus, the full script looks something like this:

 

$employees = array(’employee1@somesite.com’,’employee2@somesite.com’,’employee3@somesite.com’,’andsoon…’);

 

$startNumber = 10; // if you already have users, pick the next highest number, as it uses this for several tables below, we need them to happen at the same time so that attributes belong to specific users…

 

$db_name = “databasename”;
$db_host = “localhost”;
$db_username = “username”;
$db_password = “password”;

 

// note the nice error reporting so that we can troubleshoot connections, users, table selections, and so forth…
$link = mysql_connect($db_host, $db_username, $db_password);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
else
{
echo ‘Connected successfully<br />’;
}

 

$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) {
die (‘Can\’t use foo : ‘ . mysql_error());
}
else
{
echo ‘Selected manyvoices database successfully<br />’;
}

 

foreach($employees as $employee)
{
list($user_name, $therest) = explode(‘@’,$employee);

 

$query1 = “INSERT INTO wp_users
(id,user_login,user_pass,user_email,display_name)
VALUES
(‘” . $startNumber . “‘,'” . $user_name . “‘,md5(‘pass’),'” . $employee . “‘,'” . $user_name . “‘)
“;
//echo $query1 . “<br />”;

 

$result1 = mysql_query($query1);
if(!result1){
die(‘Error: ‘ . mysql_error());
}
else{
echo “User: ” . $user_name . ” : Added<br />”;
}

 

$query2 = “INSERT INTO wp_usermeta
(user_id,meta_key,meta_value)
VALUES
(‘” . $startNumber . “‘,’wp_capabilities’,’a:1:{s:6:\”author\”;s:1:\”1\”;}’),
(‘” . $startNumber . “‘,’wp_user_level’,’10’)
“;
//echo $query2 . “<br />”;

 

$result2 = mysql_query($query2);
if(!result2){
die(‘Error: ‘ . mysql_error());
}
else
{
echo “User Credentials: ” . $user_name . ” : Updated<br />”;
}

 

$startNumber++;
}

 

mysql_close($link);

 

 

One Response to “WordPress creating users in MySQL via script wp_user_level wp_capabilities wp_users”

  1. stoel leer Says:

    stoel leer

    alexyz» Blog Archive » WordPress creating users in MySQL via script wp_user_level wp_capabilities wp_users

Leave a Reply

You must be logged in to post a comment.