WordPress was recently ranked as the second most dreaded technology by over 50,000 developers in the 2016 Stack Overflow Developer Survey. This leads one to wonder— how can WordPress be more developer-friendly? What are the major pain points that developers are experiencing?
While there definitely won’t be a single answer for these questions, there are some things that we can do to make WordPress easier to work with. Today we’ll be looking at ways to speed up WordPress development using tools like the WP-CLI, custom shell scripts, and Alfred.
This short tutorial was written for OS X, but can be easily adapted for Linux and Windows. It also assumes that you have a basic webserver setup (MAMP or XAMPP will do), and that you have WP-CLI installed. Alfred, a productivity app for OS X, is also featured in this article, but isn’t necessary to implement most of the items covered.
Let’s get started!
Setup
Setting up a dedicated folder for your development scripts is not only cleaner than adding them to an existing directory such as /usr/local/bin/
, but it also facilitates sharing or syncing your scripts with multiple development environments.
To do this, create your custom script folder:
mkdir ~/Scripts
Then you’ll need to add the folder to your environment PATH by editing your ~/.bash_profile
file and adding the following line:
export PATH=/Users/$USER/Scripts:$PATH
Once you’ve saved the ~/.bash_profile
, you’ll need to restart your Terminal session or run source ~/.bash_profile
for the changes to take effect.
Finally, create a file called config.sh
with the following contents in your ~/Scripts
folder:
#! /bin/bash
# Path to your custom themes/plugins
PROJECT_PATH="/Users/$USER/Projects"
# Path to your WordPress installs
SITE_PATH="/Users/$USER/Sites"
# Path to your custom Scripts folder
SCRIPT_PATH="/Users/$USER/Scripts"
# Base URL (sites will be setup as subdirectories)
BASE_URL="http://localhost"
# Database information
DB_USER="root"
DB_PASS="root"
DB_HOST="localhost"
You will need to tweak the paths and database information to reflect your specific environment. Next, you’ll need to make sure it’s executable by updating the permissions:
chmod +x ~/Scripts/config.sh
Once you’ve configured this file, you’ll be able to include it in any other scripts you make so you can share config between them without duplicating code.
Automating WordPress Installs
Installing WordPress is already a quick process. The famous “5 minute install” has played a major role in it’s success. But with WP-CLI, this can easily be reduced to under 5 seconds.
Create a file named install-wp.sh
in your ~/Scripts
folder with the following contents:
#! /bin/bash
# Includes your config file
source config.sh
if [ $# -ne 1 ]; then
echo $0: usage: Destination Name
exit 1
fi
DEST=$1
# Create the database.
DB_NAME=$(echo $DEST | sed -e 's/-/_/g')
echo "Creating database $DB_NAME..."
mysql -u$DB_USER -p$DB_PASS -e"CREATE DATABASE $DB_NAME"
# Download WP Core.
wp core download --path=$SITE_PATH/$DEST
# Generate the wp-config.php file
wp core config --path=$SITE_PATH/$DEST --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --extra-php <<PHP
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
define('WP_MEMORY_LIMIT', '256M');
PHP
# Install the WordPress database.
wp core install --path=$SITE_PATH/$DEST --url=$BASE_URL/$DEST --title=$DEST --admin_user=test --admin_password=test [email protected]
Then you’ll need to make the script executable by updating the permissions:
chmod +x ~/Scripts/install-wp.sh
Now when you run the following command in Terminal:
install-wp.sh "desired-site-name"
The script should automatically download the latest version of WordPress and install the database in one simple step.
If you’re like me, you’re also deleting development environments every few days (especially when it’s so pain free to create new ones). Luckily that can be easily automated as well. Create a filed named uninstall-wp.sh
in your ~/Scripts
folder:
#! /bin/bash
# Include the config file
source config.sh
if [ $# -ne 1 ]; then
echo $0: usage: Installation name
exit 1
fi
DEST=$1
read -p "Are you sure you want to delete the files and DB for '$DEST'?" -n 1 -r
echo # Move to new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo 'Deleting files...'
# Delete files
rm -rf $SITE_PATH/$DEST/
# Delete the database.
DB_NAME=$(echo $DEST | sed -e 's/-/_/g')
echo "Deleting database $DB_NAME..."
mysql -u$DB_USER -p$DB_PASS -e"DROP DATABASE $DB_NAME"
echo 'WordPress install deleted successfully.'
fi
Again, you’ll need to make the script executable by updating the permissions:
chmod +x ~/Scripts/uninstall-wp.sh
Once you’ve done that, you can run the script like so:
uninstall-wp.sh "wp-install-to-delete"
This will delete all files and data associated with the install after confirming if you really want to go ahead with it (there is no undo button here!). If you’d rather not delete the files, you could easily modify the script to zip them up and archive them instead.
Tying it Together with Alfred
Alfred is a productivity app for OS X that helps you get things done faster by allowing you to run commands and search your computer with a simple hotkey. Think of it as an overhauled alternative to Apple’s Spotlight search.
With the paid Powerpack addon, you can even create workflows that run custom shell or Terminal scripts. For example, you could run a shell script when you enter a keyword. This is much faster than constantly manually opening Terminal, and offers other helpful features including auto-complete.
To run any of the scripts in your ~/Scripts
folder in Alfred, you’ll just need to add the >
character before the name of the script as shown in the GIF above.
You can also use Alfred to chain together common development tasks. A favorite of mine is a custom workflow to quickly run grunt
and grunt watch
.
You can set this up by creating a blank workflow called “Grunt” and add a keyword input with the keyword grunt
:
Then add a “Terminal Command” action with the following contents:
cd ~/Projects/{query} && grunt && grunt watch
The {query}
part of the command will be replaced with the argument that you provide to the command in Alfred.
Finally, connect the keyword input to the Terminal output to complete the workflow:
Now opening Alfred and typing grunt test-project
will open a new Terminal window, navigate to ~/Projects/test-project
, and run grunt && grunt watch
in the project directory.
Automate All the Things!
This barely even scratches the surface on what can be automated and improved on in a typical WordPress development workflow, but should be a good starting point for setting up your own scripts and workflows. If you regularly set aside some time to take a look at your most common tasks and see what can be automated, you’ll be surprised at how much time you’re able to save.
What development tricks or workflows have saved you a lot of time? Let us know in the comments below.