-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-rename.sh
More file actions
executable file
·65 lines (41 loc) · 1.63 KB
/
user-rename.sh
File metadata and controls
executable file
·65 lines (41 loc) · 1.63 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
#!/bin/env bash
# Renames user(s) to any given string.
#
# Use caution! The new name can be a non-standard WP username, with any characters.
source 'source/includes.sh';
# Prompt the user for the path to a CSV with username and email.
read -r -p 'Path to CSV file with users [./user-rename.csv]: ' csv_path;
# Set default value if empty string provided.
[[ -z "$csv_path" ]] && csv_path='./user-rename.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 update the user(s)
######################################################
# Init the user counter.
user_count=0;
# Loop over the entire CSV file.
while IFS="," read -r old_username new_username; do
echo "Changing username: ${old_username}";
# Ensure the user exists by getting the user ID.
if user_id=$(wp_skip_all user get "${old_username}" --field=ID >&1 ); then
# Set the SQL query for updating the current user's username and nicename.
user_update_query="UPDATE wp_users SET user_login = '${new_username}', user_nicename = '${new_username}' WHERE ID = '${user_id}'";
# Update the username and check the status of the change.
if $(wp_skip_all db query "$user_update_query" >&1 ); then
# Update the count for each successful user creation.
((user_count++));
echo "Changed to: ${new_username}";
else
# Output any errors from WPCLI from the query.
>&2
fi
else
# Output any WPCLI error related to checking if the user exists.
>&2;
fi
done < "$csv_path"
echo "Users updated: ${user_count}";