Nk_wp_admin_create

Home

Description

This is a bash function called nk_wp_admin_create() that creates an administrator account for a WordPress site using the wp-cli command-line tool.

The function takes one argument, which is the domain name of the WordPress site. If no argument is provided, it assumes that the user is currently in the WordPress install root.

The function uses the nk_docroot() function to determine the location of the WordPress installation. It then sets up two variables, wp_create_command and wp_delete_command, which are used to create and delete the lwadmin user with administrator role.

The wp_create_command variable uses the “wp user create” command to create the lwadmin user with email address lwadmin@liquidweb.com and the “–role=administrator” option to set the user’s role to administrator. It also uses the “–allow-root” option to run the command as the root user, and the “–path=$wp_location” option to specify the path to the WordPress installation.

The wp_delete_command variable uses the “wp user delete” command to delete the lwadmin user with the “–yes” option to confirm the deletion, the “–allow-root” option to run the command as the root user, and the “–path=$wp_location” option to specify the path to the WordPress installation.

The function then executes the wp_create_command to create the lwadmin user and outputs the username. It also sets up the wp_delete_command to run in 6 hours using the “at” command, and outputs a message indicating when the account will expire.

Example

[root@cloudvpsserver wp]# nk_wp_admin_create
No domain provided. Assuming we're in the WP install root.
Success: Created user 7.
Password: Sgxn(CxNwk(W#N0aeqt5(RIc
Username: lwadmin
Expires: lwadmin will expire in 6 hours.

Code

nk_wp_admin_create() {
# This sets up a Administrator account for a given WordPress site.
# First if they provided the domain name we use that as the wp install location.
if [ "$1" != "" ]; then
# Assign some variables.
# domain was provided by the user.
# wp_create_command, is the command line used to create an admin user. It relies on knowing the wp_location
# wp_delete_command, is the command line used to delete an admin user. It relies on knowing the wp_location
domain="$1"
wp_location="$(nk_docroot "$domain")"
wp_create_command="wp user create lwadmin lwadmin@liquidweb.com --role=administrator --allow-root --path=$wp_location"
wp_delete_command="wp user delete lwadmin --yes --allow-root --path=$wp_location"

# Create the lwadmin account.
$wp_create_command
# Also echo out the username as it's not provided by wp-cli output.
echo "Username: lwadmin"

# Set up the delete command to run in 6 hours.
echo "$wp_delete_command" | at -M now +6 hours 2>/dev/null
# Print out that the account will expire.
echo "Expires: lwadmin will expire in 6 hours."
# That's it we're done.
return 0
fi

# Otherwise if they did not provide a domain name.
# We'll assume they're cd'd into the install root for wp.
echo "No domain provided. Assuming we're in the WP install root."
# Basically the same variables as above, but we're using the current directory to find the the wp_location
wp_location="$(pwd)"
wp_create_command="wp user create lwadmin lwadmin@liquidweb.com --role=administrator --allow-root --path=$wp_location"
wp_delete_command="wp user delete lwadmin --yes --allow-root --path=$wp_location"
# Create the lwadmin account.
$wp_create_command
echo "Username: lwadmin"

# Set up the delete command to run in a certain amount of time.
echo "$wp_delete_command" | at -M now +6 hours 2>/dev/null
echo "Expires: lwadmin will expire in 6 hours."
}

Author: Nichole Kernreicht

Created: 2023-04-12 Wed 18:32