Tools to help you improve your flow as a WordPress developer

Part I : What is WordPress and the use of WP-CLI

Michaël Damoiseau
8 min readMay 5, 2018

Introduction

Who am I?

I am a PHP developer currently working at Buzzwoo, a digital agency located in Stuttgart (Germany) and Chiang Mai (Thailand). I am the backend team leader of the branch in Chiang Mai and work mostly with Laravel and WordPress frameworks.

What is this about?

In this series of articles, I will try to describe the tools and flows we are using at Buzzwoo for our WordPress projects. These projects range from very simple websites to e-commerce platforms and to totally custom solutions.

All the tools I will talk about will not apply to all kinds of projects, it is your job to decide what to use and in which case (do you really need unit testing for a website that is using a purchased theme?)

I have decided to split the content of this article in several parts to make it easier to digest:

  • part I : What is WordPress and the use of WP-CLI (this post)
  • part II : Testing your plugins and themes using WordPress Unit Testing (coming soon)
  • part III : Continuous Integration and WordPress development (coming soon)
  • part IV : Some improvements to our tools (coming soon)

What is WordPress?

WordPress is a CMS framework based on PHP and MySQL. It was released in May 2003 (about 15 years ago !) by Matt Mullenweg and Mike Little. Wordpress was itself a fork of the CMS b2/cafelog, this is to give you an idea of how old some parts of the code are!

When you hear negative things about WordPress, most of the time you will hear that WordPress is a blog platform that can’t scale up, and can only be used for small websites and/or blogs.

Even though it is true that WordPress started as a blog platform, and is really powerful at managing a blog site, Custom Post Types have been available in WordPress since version 3, which was released in June 2010.

Since then, WordPress has been able to manage a lot more different types of content, almost anything you may think of from an E-Commerce website storing thousands of products to a multisite platform that can host hundreds of different websites under one instance.

To give you an idea, the platform hosting all WordPress plugins is running on WordPress itself.

To summarize, WordPress is a powerful platform that can be used for many different kind of projects. I am not saying you should use WordPress for all your projects, but it can often help you, so don’t listen to people telling you that WordPress is outdated or that it sucks for any random reason.

As always, it is up to the developer to choose the best tool for the job, and hopefully after reading my article you will consider using WordPress in some of your future projects ;-)

Tools and development flow

So WordPress is a (good) platform that can deal with many different cases… That’s great, but there is a lot more that can be done with WordPress.

I have two questions for you… Most of the developers I know would answer “no” to both of them. If you too would answer “no”, then I suggest you keep on reading this article. You may find it interesting and may even learn a few things about the world of WordPress:

  1. Did you know that there is a command line tool called WP-CLI that can do almost everything inside WordPress using command line? For example, downloading and installing a new WordPress instance doesn’t take more than 30 seconds if you’re using a correct flow (more on this further down).
  2. Also, did you know that you can test your plugins and themes using Unit testing?

The tools that can be used in these two questions are nice, but they can be used in conjunction with a Continuous Integration tool such as Gitlab. Then, you will get something very powerful that will help you, as a developer, deliver some good quality, bug-free code.

WP-CLI

WP-CLI is the command line tool for WordPress. It allows you to run any command that you would run in the administration section of a WordPress website using command line only. There is no need for a web browser, and therefore makes it the perfect tool for real developers ;)

How to install it?

  1. Download it using curl (or wget): curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  2. Make the file executable: chmod +x wp-cli.phar
  3. Move it to some location that can be accessed from everywhere: sudo mv wp-cli.phar /usr/local/bin/wp

By following these steps, you should have WP-CLI available on your computer. You can test it by typing the following command line, it should display some information about WP-CLI version: wp --info

What can you do with it?

  • List all posts: wp post list
  • List all posts from a custom post type: wp post list --post_type="my_cpt" where my_cpt is the name of your custom post type.
  • Delete a post: wp post delete post_id where post_id is the ID of the post to delete.

These are all basic commands as you can see. The great thing is that you can combine them to build powerful commands.

For example, let’s say you want to delete all the posts of a given custom post type: wp post delete $(wp post list --post_type='my_cpt' --format=ids) --force where my_cpt is the name of the custom post type

Or, you would like to delete all the posts in the trash: wp post delete $(wp post list --post_status=trash --format=ids)

Download and install WordPress

There are two ways to download and install WordPress.

The first way, the tedious one, is the manual one. Let’s list all the steps to download and install WordPress:

  1. open your browser
  2. go to https://www.wordpress.org
  3. download the file
  4. unzip it
  5. create and configure your database using PHPMyAdmin (adds a few extra steps)
  6. go to your WordPress url and configure your website :
  7. select your language
  8. enter your database credentials
  9. run the installation (title of the website, administrator user credentials)
  10. log in with your new admin account

As you can see there are quite a few steps to execute to have a WordPress up and running (I haven’t counted the steps for the database creation, but you get the idea).

We will now try to improve this by using WP-CLI. This will be the second way, kind of automated in a sense that everything will be done through command line.

  1. Open your terminal
  2. Create your database: mysql -uDB_USER -pDB_PASSWORD -e "CREATE DATABASE IF NOT EXISTS DB_NAME DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
    Where:
    - DB_USER is your database user
    - DB_PASSWORD is your database password
    - DB_NAME is your database name
  3. Go to the folder where you want to store your WordPress installation
  4. Download WordPress: wp core download
  5. Configure your database credentials: wp core config --dbname=DB_NAME --dbuser=DB_USER --dbpass=DB_PASSWORD
    Where:
    - DB_NAMEis the name of the database
    - DB_USER your database user
    -DB_PASSWORD the password of your database user.
  6. Configure your WordPress instance: wp core install — url=http://localhost--title=WordPress --admin_user=mike --admin_password=MySuperPassword --admin_email=mike@example.com

That’s it! You know have a WordPress ready to be used! And you know what? If you are like me, the first thing you will do is deactivate the default themes and plugins, so let’s do this now:

  1. wp plugin uninstall hello --deactivate && wp plugin uninstall akismet --deactivate && wp theme delete twentyfourteen && wp theme delete twentysixteen

Wow! Now we have a clean WordPress install ready to be used!

By the way, you just saw that we can execute multiple commands in one line by using the special separator &&. I’ll let you search about it if you want to know more ;)

Automate the installation

We have seen that it is pretty easy to install and configure and new Wordpress installation. One thing I don’t like though is that I have to remember all the commands, and I am very bad at it. Also, I often type wrong some parameters and this can be annoying.

So instead, why not spend some time now to automate the process as much as possible, so that we can win some precious time every single time we have to prepare a new WordPress installation?

When I start working on a new project, I always use the following template. This makes the whole installation a lot easier. I have two folders: htdocs for the website and development for the developer tools.

The folder structure of my projects looks like this:

/var/www/project-name
|__ /htdocs
|__ /development

To automate the installation process, we will write a small shell script. Don’t worry if you’re not familiar with the syntax, it is pretty straightforward. The script will be saved in the file development/setup.sh.
For those of you who are on Windows, you should create a .BAT file. I don’t have much experience with the BAT format, so let’s keep this for a future update of the article.

A shell script must start with the following line:

#!/bin/bash

Let’s think of the parameters that must have different values for each installation. I can think of two sets of values here: the database credentials and the settings of the website.

There are two ways we can handle this: by using parameters on the command line when executing the script, or by using variable inside the script. I prefer the second option because the first one requires the developer to remember the name of all the parameters, and I already said I was bad at it so let’s go for option 1…

#####
# These are the variables you must change for each of your project:
#####
PROJECT=”project_name”
WP_USER=”admin_name”
WP_PASSWORD=”admin_password”
WP_EMAIL=”admin@example.com”
WP_TITLE==”WordPress”
DB_USER=”root”
DB_PASSWORD=”root_password”
DB_HOST=”localhost”

The script is located in the folder development, so we have to make sure we are running the WP-CLI commands from the correct folder (htdocs):

#root directory
echo “Start script: change to project-root directory”
cd ..

Create the datbase:

#create database
mysql -uD$B_USER -p$DB_PASSWORD -e “CREATE DATABASE IF NOT EXISTS \`${PROJECT}\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;”

Now we can run the different command lines to download, configure and install our WordPress instance:

# Install and create WordPress config file
wp core download
wp core config — dbname=$PROJECT — dbuser=$DB_USER — dbpass=$DB_PASSWORD
wp core install — url=$PROJECT.dev — title=$WP_TITLE — admin_user=$WP_USER — admin_password=$WP_PASSWORD –admin_email=$WP_EMAIL

Little extra, we don’t need the default plugins and themes from WordPress, so let’s remove them directly:

# Uninstall unnecessary plugins
wp plugin uninstall hello --deactivate
wp plugin uninstall akismet –deactivate
# Uninstall unnecessary themes
wp theme delete twentyfourteen
wp theme delete twentysixteen

We are done with the plugin, let’s display a nice message to the user:

echo "DONE!"

Now that we have a nice script ready to be used, let’s add the final touch to it (means make it executable). This is a command line:

chmod +x development/setup.sh

and execute the setup script:

cd development
./setup.sh

Hopefully you shouldn’t have any error messages :)

For the sack of clarity, I will add the full content of the script here:

#!/bin/bash#####
# These are the variables you must change for each of your project:
#####
PROJECT="project_name"
WP_USER="admin_name"
WP_PASSWORD="admin_password"
WP_EMAIL="admin@example.com"
WP_TITLE=="WordPress"
DB_USER="db_user"
DB_PASSWORD="db_password"
DB_HOST="localhost"
#root directory
echo "Start script: change to project-root directory"
cd ..
#create database
mysql -uroot -pbuzzwoo -e "CREATE DATABASE IF NOT EXISTS \`${PROJECT}\` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
# Install and create WordPress config file
wp core download
wp core config --dbname=$PROJECT --dbuser=$DB_USER --dbpass=$DB_PASSWORD
wp core install --url=$PROJECT.dev --title=$WP_TITLE --admin_user=$WP_USER --admin_password=$WP_PASSWORD --admin_email=$WP_EMAIL
# Uninstall unnecessary plugins
wp plugin uninstall hello --deactivate
wp plugin uninstall akismet –deactivate
# Uninstall unnecessary themes
wp theme delete twentyfourteen
wp theme delete twentysixteen
echo "DONE!"

Conclusion

Hopefully you have learned a few things after reading this article.

We have learned about the command line tool from WordPress: WP-CLI and see how handy it could be. We have also seen how to automate the installation of a new instance of WordPress using WP-CLI and a small shell script.

Using this method, creating a new instance of a WordPress website doesn’t take more than 30 seconds. There are of course some extra steps that are needed (e.g. configuring your Apache/Nginx) webserver, installing WP-CLI if the binary is not available on your server, etc. All this will be done later, in another part of this article.

That’s it for now. I hope you have enjoyed reading the article and feel free to contact me if you have any suggestions and/or corrections. Cheers!

--

--

Michaël Damoiseau

I am a PHP developer currently working at Buzzwoo as backend team leader in Chiang Mai, Thailand. I work mostly with Laravel and WordPress on a Mac.