Skip to content

Instantly share code, notes, and snippets.

@infn8
Last active July 1, 2020 22:30
Show Gist options
  • Save infn8/884e178841bc460aa689 to your computer and use it in GitHub Desktop.
Save infn8/884e178841bc460aa689 to your computer and use it in GitHub Desktop.
Wp CLI New Wordpress Install

New Wordpress Shell Script

  • @param --dbname:
    • Used to set the database name.
    • Sets param of same name for wp core config command from WP CLI
    • Leave blank to be prompted for input
  • @param --dbuser:
    • Used to set the database user.
    • Sets param of same name for wp core config command from WP CLI
    • Leave blank to be prompted for input
  • @param --dbpass:
    • Used to set the database user's password.
    • Sets param of same name for wp core config command from WP CLI
    • Leave blank to be prompted for input
  • @param --url:
    • The address of the new site.
    • Sets param of same name for wp core install command from WP CLI
    • Leave blank to be prompted for input
  • @param --title:
    • The title of the new site.
    • Sets param of same name for wp core install command from WP CLI
    • Leave blank to be prompted for input
  • @param --admin_user:
    • The name of the admin user.
    • Sets param of same name for wp core install command from WP CLI
    • Leave blank to be prompted for input
  • @param --admin_password:
    • The password for the admin user.
    • Sets param of same name for wp core install command from WP CLI
    • Leave blank to be prompted for input
  • @param --admin_email:
    • The email address for the admin user.
    • Sets param of same name for wp core install command from WP CLI
    • Leave blank to be prompted for input
  • @param --db_exists:
    • If param exists the command wp db create will be skipped
  • @param --clear_dir:
    • If param exists the command rm -rf * will be called. Deleting all the contents of the current folder. Carefull!!!

USAGE

Firstly Install the WP CLI as described at http://wp-cli.org/.

Ensure that you can run the CLI by typing:

wp --info

If that works you can proceed. Download the shell file from this gist into the dir you'd like to use. Make the shell file executable by typing.

chmod +x NewWordpress.sh

Then Run a command like this:

./NewWordpress.sh --dbname 'avengers_assemble' --dbuser 'NickFury' --url 'http://localhost/AvengersAssemble' --title 'Avengers Assemble' --admin_user 'OneEyedNick' --admin_email '[email protected]'

That sets all params except --admin_password and --dbpass Nick Fury keeps his passwords secret.

Like a boss.

If you wanna really run things like a boss then toss the script in a directory of your choosing and alter your bash profile:

sudo nano ~/.bash_profile

and add the following text:

alias newwp="/path/to/NewWordpress.sh"

then logout of bash or type source ~/.bash_profile. after that you can run this command by just typing newwp and following the prompts or by editing the example above like so:

newwp --dbname 'avengers_assemble' --dbuser 'NickFury' --url 'http://localhost/AvengersAssemble' --title 'Avengers Assemble' --admin_user 'OneEyedNick' --admin_email '[email protected]'

#!/bin/bash
for ((i=1;i<=$#;i++));
do
if [ ${!i} = "--dbname" ]
then ((i++))
dbname=${!i};
elif [ ${!i} = "--dbuser" ];
then ((i++))
dbuser=${!i};
elif [ ${!i} = "--dbpass" ];
then ((i++))
dbpass=${!i};
elif [ ${!i} = "--url" ];
then ((i++))
url=${!i};
elif [ ${!i} = "--title" ];
then ((i++))
title=${!i};
elif [ ${!i} = "--admin_user" ];
then ((i++))
admin_user=${!i};
elif [ ${!i} = "--admin_password" ];
then ((i++))
admin_password=${!i};
elif [ ${!i} = "--admin_email" ];
then ((i++))
admin_email=${!i};
elif [ ${!i} = "--db_exists" ];
then
db_exists="yes"
elif [ ${!i} = "--clear_dir" ];
then
echo "Flag --clear_dir exists. Removing all direcory files"
rm -rf *
fi
done;
if [ "$dbname" = "" ]; then
echo "Parameter --dbname not set. Please enter it now."
read dbname
fi
if [ "$dbuser" = "" ]; then
echo "Parameter --dbuser not set. Please enter it now."
read dbuser
fi
if [ "$dbpass" = "" ]; then
echo "Parameter --dbpass not set. Please enter it now."
read dbpass
fi
if [ "$url" = "" ]; then
echo "Parameter --url not set. Please enter it now."
read url
fi
if [ "$title" = "" ]; then
echo "Parameter --title not set. Please enter it now."
read title
fi
if [ "$admin_user" = "" ]; then
echo "Parameter --admin_user not set. Please enter it now."
read admin_user
fi
if [ "$admin_password" = "" ]; then
echo "Parameter --admin_password not set. Please enter it now."
read admin_password
fi
if [ "$admin_email" = "" ]; then
echo "Parameter --admin_email not set. Please enter it now."
read admin_email
fi
if [ "$db_exists" != "yes" ]; then
db_exists="no"
fi
echo "Downloading Core:"
wp core download
echo "Generating Config File:"
wp core config --dbname="$dbname" --dbuser="$dbuser" --dbpass="$dbpass" --skip-check
echo "db_exists flag set to: $db_exists"
if [ "$db_exists" = "no" ]; then
echo "Creating DB"
wp db create
fi
echo "Running Core Install"
wp core install --url="$url" --title="$title" --admin_user="$admin_user" --admin_password="$admin_password" --admin_email="$admin_email"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment