If you're an avid WordPress user, you know that setting up a new WordPress site can be quite a chore, involving multiple manual steps like downloading core files, configuring the database, and installing essential plugins. To streamline this process, you can create a custom function in your shell configuration (zshrc
or bashrc
) that automates the entire WordPress installation with a single command.
Before we dive into creating the function, ensure you have the following prerequisites:
- A Linux or macOS system.
- WordPress Command Line Interface (WP-CLI) installed on your system.
- Visual Studio Code (VS Code) or any code editor of your choice installed.
The wpinstall
function automates WordPress installation by performing the following steps:
- Downloads WordPress core files.
- Configures the database.
- Installs WordPress.
- Sets up the front page.
- Installs and activates essential plugins.
- Generates a secure URL and login URL.
Here's the code for the wpinstall
function:
# _________________
# Install WordPress with a single command.
# _________________
wpinstall() {
if [ "$1" != "" ]; then
# Clear the terminal screen
clear
# Installation directory
install_dir="$1"
# Download WordPress core files
wp core download --path="$install_dir"
# Move to the installation directory
cd "$install_dir"
# Create wp-config file
wp config create --dbname="$install_dir"
# Create the database and install WordPress
wp db create
wp core install --url="https://$install_dir.test"
# Discourage search engines
wp option update blog_public 0
# Delete the sample page and create a homepage
sample_page_id=$(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids)
wp post delete "$sample_page_id" --force
wp post create --post_type=page --post_title=Home --post_status=publish --post_author=1
# Set the homepage as the front page
wp option update show_on_front 'page'
# Set the homepage as the new page
home_page_id=$(wp post list --post_type=page --post_status=publish --posts_per_page=1 --pagename=home --field=ID --format=ids)
wp option update page_on_front "$home_page_id"
# Delete inactive plugins and themes
wp theme delete $(wp theme list --status=inactive --field=name)
wp plugin delete $(wp plugin list --status=inactive --field=name)
# Install and activate plugins
wp plugin install all-in-one-wp-migration wp-mail-smtp query-monitor --activate
# Clear the terminal screen
clear
# Determine the length of install_dir for dynamic formatting
install_dir_length=${#install_dir}
# Calculate the padding for the first row (39 - length of install_dir)
padding_first_row=$((30 - install_dir_length))
# Calculate the padding for the second row (29 - length of install_dir)
padding_second_row=$((20 - install_dir_length))
# Create dynamic padding based on the calculated values
padding_first=$(printf ' %.0s' $(seq 1 $padding_first_row))
padding_second=$(printf ' %.0s' $(seq 1 $padding_second_row))
# ASCII Table
echo "|=================================================================|"
echo "| WordPress Installation Complete |"
echo "|-----------------------------------------------------------------|"
printf "| Site URL: | https://%s.test%s|\n" "$install_dir" "$padding_first"
echo "|-----------------------------------------------------------------|"
printf "| Login URL: | https://%s.test/wp-admin/%s|\n" "$install_dir" "$padding_second"
echo "|-----------------------------------------------------------------|"
# Open the wp-config.php in a code editor
code ./wp-config.php
# Link with Valet and secure it(Note: I use valet, if you're not using valet then remove this line.)
valet link && valet secure
else
echo "Error: Please provide a sitename as a parameter. Example: wpinstall mywebsite"
fi
}
To use the wpinstall
function, follow these steps:
-
Open your shell configuration file (
zshrc
for Zsh orbashrc
for Bash) using your preferred code editor. For example, you can use VS Code:code ~/.zshrc
or
code ~/.bashrc
-
Scroll to the end of the file and paste the
wpinstall
function code. -
Save the file and exit your code editor.
-
Source your shell configuration file to apply the changes:
source ~/.zshrc # For Zsh
or
source ~/.bashrc # For Bash
-
Now, you can run the
wpinstall
function from your terminal with a site name as a parameter. For example:wpinstall mywebsite
Replace
mywebsite
with your desired site name. Use hyphens (-) to separate words; avoid spaces. -
The script will automate the entire WordPress installation process, and once complete, it will display the site URL and login URL.
-
Access your new WordPress site by visiting the provided URLs.
By adding the wpinstall
function to your shell configuration, you can effortlessly automate WordPress installations with a single command. This streamlined process saves you time and simplifies the setup of new WordPress sites. Enjoy the convenience of quick and easy WordPress installations directly from your terminal!