Skip to content

Instantly share code, notes, and snippets.

@iisti
Created October 26, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iisti/e1365a37471a865b832d7dcf3b52d677 to your computer and use it in GitHub Desktop.
Save iisti/e1365a37471a865b832d7dcf3b52d677 to your computer and use it in GitHub Desktop.
This bash script adds a new administrator user to WordPress' MySQL database.
#!/usr/bin/env bash
# This bash script adds a new administrator user to WordPress' MySQL database.
echo -n "Give a new admin user login name: "
read userlogin
echo -n "Email address for the user $userlogin: "
read useremail
# Ask for the password two times and check that they match
while true; do
read -s -p "Give password for $userlogin: " userpw
echo ""
read -s -p "Write the password again: " userpw2
if [ "$userpw" == "$userpw2" ]; then
echo ""
echo "Passwords match."
break
fi
echo ""
echo "Passwords do not match! Give the password again."
done
echo "Inserting new administrator user $userlogin to WordPress database."
echo -n "Give WordPress database name: "
read wpdatabase
echo -n "Give WordPress database's username: "
read wpdatabaseuser
# Insert new admin user to MySQL database and store output to a variable
mysql_output="$(echo "INSERT INTO \`wp_users\` (\`user_login\`, \`user_pass\`, \`user_nicename\`, \`user_email\`, \`user_status\`, \`user_registered\`, \`display_name\`) VALUES ('$userlogin', MD5('$userpw'), '$userlogin', '$useremail', '0', now(), '$userlogin');
INSERT INTO \`wp_usermeta\` (\`umeta_id\`, \`user_id\`, \`meta_key\`, \`meta_value\`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:\"administrator\";s:1:\"1\";}');
INSERT INTO \`wp_usermeta\` (\`umeta_id\`, \`user_id\`, \`meta_key\`, \`meta_value\`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
INSERT INTO \`wp_usermeta\` (\`umeta_id\`, \`user_id\`, \`meta_key\`, \`meta_value\`) VALUES (NULL, (Select max(id) FROM wp_users), 'nickname', '$userlogin');" | mysql -u$wpdatabaseuser -p $wpdatabase 2>&1 > /dev/null)"
# There should not be any output. So if there's output there probably was an error.
if [ -n "$mysql_output" ]; then
echo $mysql_output
else
echo "Creation ready. Try to login with the new user account."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment