Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayhardaha/188dfad9b2b893f6df4dddb25ecdb68a to your computer and use it in GitHub Desktop.
Save vijayhardaha/188dfad9b2b893f6df4dddb25ecdb68a to your computer and use it in GitHub Desktop.
Automate WordPress Installation in Your Terminal

Automate WordPress Installation in Your Terminal

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.

Prerequisites

Before we dive into creating the function, ensure you have the following prerequisites:

The wpinstall Function

The wpinstall function automates WordPress installation by performing the following steps:

  1. Downloads WordPress core files.
  2. Configures the database.
  3. Installs WordPress.
  4. Sets up the front page.
  5. Installs and activates essential plugins.
  6. 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
}

Using the wpinstall Function

To use the wpinstall function, follow these steps:

  1. Open your shell configuration file (zshrc for Zsh or bashrc for Bash) using your preferred code editor. For example, you can use VS Code:

    code ~/.zshrc

    or

    code ~/.bashrc
  2. Scroll to the end of the file and paste the wpinstall function code.

  3. Save the file and exit your code editor.

  4. Source your shell configuration file to apply the changes:

    source ~/.zshrc  # For Zsh

    or

    source ~/.bashrc  # For Bash
  5. 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.

  6. The script will automate the entire WordPress installation process, and once complete, it will display the site URL and login URL.

  7. Access your new WordPress site by visiting the provided URLs.

Conclusion

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment