Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This script installs WordPress inside a sub directory
# Installation:
# Install the script in one of the folders in your PATH. Make sure it has execute permissions (i.e. chmod +x wp-install-core-sub-dir).
# $ mkdir mysite
# $ cd mysite
# $ wp-install-core-sub-dir
# This is a simple script as an example, it could be improved by accepting parameters etc.
SITE_TITLE='WordPress CLI Test Site'
# create the dir for the core files
mkdir $CORE_DIR
# download WordPress files
wp core download
cd ../
# create the wp-config.php file
wp core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --path=$CORE_DIR
# create the dabase
wp db create
# install WordPress (less than 5 mins)
wp core install --url=$SITE_URL --title="$SITE_TITLE" --admin_user=$SITE_USER --admin_password=$SITE_PASS --admin_email=$SITE_EMAIL --path=$CORE_DIR
# Copy (not move) index.php file to root
cp "$CORE_DIR/index.php" ./index.php
# Edit index.php to point to correct path of wp-blog-header.php
sed -i '' "s/\/wp-blog-header/\/$CORE_DIR\/wp-blog-header/g" index.php
# Update the siteurl in the database with sub directory path
wp option update siteurl $(wp option get siteurl)/$CORE_DIR
# Uncomment the below line if you want the config in root
#cp "$CORE_DIR/wp-config.php" ./wp-config.php
echo 'Install finished!'

This comment has been minimized.

Copy link

danelige commented Oct 17, 2018

Iain, thank you! few corrections that were needed to make this script work on my cPanel setup.

  1. line 47: sed -i '' ... drop the ''

  2. line 50: wp option update siteurl $(wp option get siteurl)/$CORE_DIR you need to add the path here too: wp option update siteurl $(wp option get siteurl --path=$CORE_DIR)/$CORE_DIR --path=$CORE_DIR

  3. in order to prep empty database and user in a cPanel environment, use UAPI command like this:

# cPanel API: This creates DB and USER in such a way that cPanel can track this new db and user
uapi --output=json Mysql create_database name="$DB_NAME"
uapi --output=json Mysql create_user name="$DB_USER" password="$DB_PASS"
uapi Mysql set_privileges_on_database user="$DB_USER" database="$DB_NAME" privileges=ALL%20PRIVILEGES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.