-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-create.sh
More file actions
executable file
·84 lines (55 loc) · 1.97 KB
/
user-create.sh
File metadata and controls
executable file
·84 lines (55 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/env bash
# Creates users from a CSV. On multisite, adds user to every site.
#
# CSV expects to have the following values with no header row as labels:
# username
# email
# role (a valid WP role)
# superadmin (1 or 0)
source 'source/includes.sh';
# Check if this is a multisite/network installation of WordPress.
is_multisite=$(wp_skip_all config get MULTISITE);
echo;
if [ "$is_multisite" == 1 ]; then
echo 'Multisite detected';
echo ' Users will be added to every network site';
else
echo 'Multisite NOT detected';
fi
echo;
# Prompt the user for the path to a CSV with username and email.
read -r -p 'Path to CSV file with users [./user-create.csv]: ' csv_path;
# Set default value if empty string provided.
[[ -z "$csv_path" ]] && csv_path='./user-create.csv';
# Ensure the file actually exists.
if [[ ! -f "$csv_path" ]]; then
echo "The file you provided does not exist: ${csv_path}";
exit 1;
fi
######################################################
# Read the CSV and create the user(s)
######################################################
# Init the user counter.
user_count=0;
# Loop over the entire CSV file.
while IFS="," read -r username email role superadmin; do
# Create the user. Only output WPCLI success message on successful creation.
if new_user_id=$(wp_skip_all user create --porcelain "$username" "$email" --role="$role" >&1 ); then
# Update the count for each successful user creation.
((user_count++));
echo "'$username' was successfully created.";
# Check if the user should be a superadmin.
if [[ "1" == "$superadmin" ]]; then
wp_skip_all super-admin add $username;
else
for site_url in $(wp_skip_all site list --field="url" --archived=0 --deleted=0 --spam=0); do
# The user is not a superadmin so add them to each site individually.
wp_on_site user set-role "$new_user_id" "$role";
done;
fi
else
# Output any errors from WPCLI for the user creation.
>&2
fi
done < "$csv_path"
echo "Users added: ${user_count}";