Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt Script to Install WordPress. #5747

Closed
owaisahmed5300 opened this issue Mar 14, 2023 · 1 comment
Closed

Prompt Script to Install WordPress. #5747

owaisahmed5300 opened this issue Mar 14, 2023 · 1 comment

Comments

@owaisahmed5300
Copy link

owaisahmed5300 commented Mar 14, 2023

it would be greatly appreciated if the wp-cli bundle could include this script. It streamlines the process of installing and downloading WordPress, making it much faster and easier.

The script automates the installation of WordPress in the current working directory by downloading WordPress core, prompting the user to enter necessary installation details, updating the wp-config.php file, creating the MySQL database and user, and finally installing WordPress. Additionally, the script generates default values for the user to accept or modify for the installation details. These details include the MySQL username and password, database host, database name, table prefix, site URL, site title, admin username and password, and admin email.

If there is any further room for improvement, we would be happy to work on enhancing the script. The primary goal is to simplify the process of WordPress downloading and installation, making it possible with a single command.

wp-install sh file

# Define the installation directory as the current working directory
INSTALL_DIR=$(pwd)
DIR_NAME=$(basename "$PWD")
DOMAIN_NAME=$(echo "$DIR_NAME" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-')
DOMAIN_NAME="$DOMAIN_NAME.com"

# Download WordPress core
wp core download

# Prompt the user to enter necessary installation details
echo "WordPress installation:"
read -p "Enter your MySQL username (default: root): " DB_USER
DB_USER=${DB_USER:-root}
read -p "Enter your MySQL password: " DB_PASS
read -p "Enter your database host (default: localhost): " DB_HOST
DB_HOST=${DB_HOST:-localhost}
read -p "Enter your database name (default: ${DIR_NAME//-/_}): " DB_NAME
DB_NAME=${DB_NAME:-${DIR_NAME//-/_}}
read -p "Enter your table prefix (default: wp_): " TABLE_PREFIX
TABLE_PREFIX=${TABLE_PREFIX:-wp_}
read -p "Enter your site URL (default: http://localhost/$DIR_NAME): " SITE_URL
SITE_URL=${SITE_URL:-http://localhost/$DIR_NAME}
read -p "Enter your site title (default: $(echo "$DIR_NAME" | tr '-' ' ' | tr '_' ' ' | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')): " SITE_TITLE
SITE_TITLE=${SITE_TITLE:-$(echo "$DIR_NAME" | tr '-' ' ' | tr '_' ' ' | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')}
read -p "Enter the admin username (default: admin): " ADMIN_USER
ADMIN_USER=${ADMIN_USER:-admin}
read -p "Enter the admin password (default: 12345): " ADMIN_PASS
ADMIN_PASS=${ADMIN_PASS:-12345}

# Delete wp-config.php file if it exists
if [ ! -f "$INSTALL_DIR/wp-config.php" ]; then
  cp "$INSTALL_DIR/wp-config-sample.php" "$INSTALL_DIR/wp-config.php"

  # Update the configuration file with the user-entered details
  sed -i "s/database_name_here/$DB_NAME/g" "$INSTALL_DIR/wp-config.php"
  sed -i "s/username_here/$DB_USER/g" "$INSTALL_DIR/wp-config.php"
  sed -i "s/password_here/$DB_PASS/g" "$INSTALL_DIR/wp-config.php"
  sed -i "s/localhost/$DB_HOST/g" "$INSTALL_DIR/wp-config.php"
  sed -i "s/wp_/$TABLE_PREFIX/g" "$INSTALL_DIR/wp-config.php"
fi

# Create MySQL database and user
if [ -z "$DB_PASS" ]; then
  mysql -u "$DB_USER" -h "$DB_HOST" <<MYSQL_SCRIPT
  CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;
  GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '$DB_USER'@'$DB_HOST';
  FLUSH PRIVILEGES;
MYSQL_SCRIPT
else
  mysql -u "$DB_USER" -p"$DB_PASS" -h "$DB_HOST" <<MYSQL_SCRIPT
  CREATE DATABASE IF NOT EXISTS \`$DB_NAME\`;
  GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '$DB_USER'@'$DB_HOST' IDENTIFIED BY '$DB_PASS';
  FLUSH PRIVILEGES;
MYSQL_SCRIPT
fi

# Install WordPress
wp core install --url="$SITE_URL" --title="$SITE_TITLE" --admin_user="$ADMIN_USER" --admin_password="$ADMIN_PASS" --admin_email="admin@$DOMAIN_NAME" --skip-email --path="$INSTALL_DIR"

echo "WordPress installation completed!"


wp-install.bat (in same directory).

@echo OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/../wp-cli/wp-cli/bin/wp-install
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
sh "%BIN_TARGET%" %*

@danielbachhuber
Copy link
Member

Thanks for the suggestion, @owaisahmed5300 !

Feel free to share your script on this existing issue: wp-cli/ideas#19

If/when we ever implement this, we'll want to do so as a WP-CLI command.

@danielbachhuber danielbachhuber closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants