Script a WordPress Valet Install on macOS – bash/wp-cli/wp-cli-valet
By creating a bash script with wp-cli and wp-cli-valet commands on macOS you can create a streamlined and tailored WordPress install using Valet on your local development, this reduces repetitious tasks after a site installation – similar to a blueprint in other WP local developments.
The type of tasks may include:
- Adding and activating a theme
- Removing default themes
- Adding and activating plugins
- Removing default plugins
- Adding license keys for premium plugins
- Setting up WP Debug
First up is to have all the components installed: Valet, wp-cli and wp-cli-valet – then use a bash script which when added to your shell path can run and complete the WordPress installation in your sites folder which should be registered using the valet park command, see the linked article first.
#! bash script
#!/bin/bash # Ref wp-cli-valet - https://github.com/aaemnnosttv/wp-cli-valet-command # Set up Terminal styles VP_NONE='\033[00m' VP_RED='\033[01;31m' VP_GREEN='\033[01;32m' VP_YELLOW='\033[01;33m' VP_PURPLE='\033[01;35m' VP_CYAN='\033[01;36m' VP_WHITE='\033[01;37m' VP_BOLD='\033[1m' VP_UNDERLINE='\033[4m' # Change current directory to Sites cd ~/Sites/ # Create WordPress site wp valet new $1 --dbname="wp_$1" --dbuser="root" --dbpass="" --dbprefix="wp_" --admin_user="admin" --admin_password='password' --admin_email="[email protected]" --unsecure # Site creation success message echo -e "${VP_GREEN}Success:${VP_NONE} $1 created" # Make https - I have hashed this as I want it non-https # valet secure $1 # Navigate to themes directory cd ~/Sites/ cd $1/wp-content/themes # Copy all the content from my folder ~ this has themes and plugins which will be moved into place further down in the script cp -a ~/Sites/zips/*.zip . unzip '*.zip' rm -rf __MACOSX/ rm *.zip # Clone my GitHub Theme & Gulp Task runner git clone https://github.com/yourname/mytheme.git mytheme cd mytheme rm -rf .git/ git clone https://github.com/yourname/gulptasker.git geegulp mv gulptasker/gulpfile.js . mv gulptasker/package.json . rm -rf gulptasker/ # Activate my theme wp theme activate mytheme # Delete 2015, 2016, 2017 WordPress Default Themes wp theme delete twentyfifteen twentysixteen twentyseventeen # Navigate to plugins directory cd ~/Sites/ cd $1/wp-content/plugins # Copy all the content from my premium plugin folder cp ../themes/lib/plugins/premium-plugin1.zip . cp ../themes/lib/plugins/premium-plugin2.zip . unzip '*.zip' rm -rf __MACOSX/ rm *.zip # Delete Hello Dolly - (but keeping Akismet) wp plugin delete hello #wp plugin delete akismet # Update all plugins wp plugin update --all # Activate all plugins wp plugin activate --all # Register my Beaver wp beaver register --license=XXXXXXXX # Launch the site in default browser ? valet open $1
The only variable in the script is $1 ~ which is the sitename and its value is passed in when you run the script on the command line – so if the script is called wpcreate.sh you would run in the Terminal in the Sites directory
wpcreate mynewsite
This will execute the script and pass in the value you gave to all instances of $1 in the script.
Setting Up Constants in wp-config.php
To add license keys for plugins and other WP constants in the wp-config.php file – you can do so in the config.yml file in your home directory filed as so in the following:
/Users/yourname/.wp-cli/config.yml
Your config file can be something like…
valet new: admin_user: admin admin_password: "password" admin_email: [email protected] locale: en_AU # Subcommand defaults (e.g. `wp config create`) config create: dbuser: root dbpass: extra-php: | define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); @ini_set( 'display_errors', 0 ); define( 'WPCOM_API_KEY', 'XXXXX' ); define( 'GF_LICENSE_KEY', 'XXXXX' );
So as well as the WP Admin credentials, the locale is set and Debug preferences set as well as two license keys for Akismet and Gravity Forms.
So now the WordPress install is more complete and has less administrative work to do saving you more time.
Ref & Ref & Ref.