Skip to content

Commit 525ccf2

Browse files
committed
feat(user creation): initial commit of user creation script
1 parent 2127369 commit 525ccf2

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

user-create.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/env bash
2+
3+
# Creates users from a CSV. On multisite, adds user to every site.
4+
#
5+
# CSV expects to have the following values with no header row as labels:
6+
# username
7+
# email
8+
# role (a valid WP role)
9+
# superadmin (1 or 0)
10+
11+
source 'source/includes.sh';
12+
13+
# Check if this is a multisite/network installation of WordPress.
14+
is_multisite=$(wp config get MULTISITE);
15+
16+
echo;
17+
18+
if [ "$is_multisite" == 1 ]; then
19+
echo 'Multisite detected';
20+
echo ' Users will be added to every network site';
21+
else
22+
echo 'Multisite NOT detected';
23+
fi
24+
25+
echo;
26+
27+
# Prompt the user for the path to a CSV with username and email.
28+
read -r -p 'Path to CSV file with users [./user-create.csv]: ' csv_path;
29+
30+
# Set default value if empty string provided.
31+
[[ -z "$csv_path" ]] && csv_path='./user-create.csv';
32+
33+
# Ensure the file actually exists.
34+
[[ ! -f "$csv_path" ]]; then
35+
36+
echo "The file you provided does not exist: ${csv_path}";
37+
exit 1;
38+
39+
fi
40+
41+
######################################################
42+
# Read the CSV and create the user(s)
43+
######################################################
44+
45+
# Init the user counter.
46+
user_count=0;
47+
48+
# Loop over the entire CSV file.
49+
while IFS="," read -r username email role superadmin; do
50+
51+
# Create the user.
52+
new_user_id=$(wp_skip_all user create --porcelain "$username" "$email" --role="$role");
53+
54+
# Check if the user should be a superadmin.
55+
if [[ "1" == "$superadmin" ]]; then
56+
57+
wp super-admin add $username;
58+
59+
fi
60+
61+
# Update the count for each successful user creation.
62+
[[ new_user_id > 0 ]] && ((user_count++));
63+
64+
done < "$csv_path"
65+
66+
echo "Users added: ${user_count}";

0 commit comments

Comments
 (0)