Advanced WordPress Development: Using the Command Line

Advanced WordPress Development: Using the Command Line

While mention of the command line strikes fear into the hearts of many developers, the truth is that it is an easy to use and immensely helpful tool for development.

Once you understand how it works, the command line is straightforward to use and will streamline how you work, saving you a bunch of time.

This is the fourth post in our six-part series focusing on WordPress for advanced developers. This series follows on from our popular WordPress Development for Intermediate Users, which introduced you to some meaty coding topics, including theme development in detail, making themes customizer-ready, building plugins, custom post types and taxonomies, queries and loops, custom fields and metadata, and localization.

In this tutorial, I’ll show you how you can use the command line to perform common tasks and make your life so much easier.

Note: It’s important that you have a working knowledge of PHP as this is the foundational language of WordPress for this series, which covers advanced topics aimed at developers. I’ll be referring to code snippets throughout this series.

What is the Command Line?

Back in the good old days, there was no real user interface. You could only get things done by issuing text-based commands using the terminal. Nowadays, we don’t have terminals – we have highly integrated computers that come with terminal emulators that allow us to issue commands using a graphical user interface.

Commands can do anything from resizing an image to creating new files and switching between monitors. Anything you do via a user interface could potentially be done with commands issued in the terminal.

What makes commands even more useful is that they can be grouped together to perform many complex tasks in one go. You could write a very short script using existing commands that, when issued, would crop and optimize all images in a directory and then add them to a single zip file.

Do I Need the Terminal?

Up to a certain point, you don’t need to use the terminal, it just makes your life easier by allowing you to accomplish complex tasks a lot quicker. After a while you will come across scenarios where you must use the terminal.

A good example is accessing remote servers using SSH or coding anything in Node where you’ll need to use npm to install packages and issue commands.

In a nutshell: the terminal is not a requirement for WordPress development work, but is an amazingly helpful tool to learn that will open new horizons and prepare you to work with languages like Node.

Windows, Unix and OSX Terminals

If you’re on a Unix-based system like Ubuntu, CentOS or OSX, you don’t really need to worry about setting up the command line. Apart from some minor differences, which you won’t bump into for a while, commands are all the same and many useful ones come bundled with your operating system.

Windows is another matter. Instead of a terminal it has the command prompt, which is far less useful for our purposes. I recommend installing Cygwin, which will provide many useful commands that we’ll use in this tutorial. If you need help with installation check out this handy installation guide.

Getting Started: Terminal Basics

From here on out I will presume that you’ve installed Cygwin and you have access to most commands that Linux and OSX users have.

When you launch a terminal window you’ll notice that it contains a fair bit of information.

Basic OSX Terminal
Basic OSX terminal.

If yours looks a bit different, don’t worry about it, it will still work just fine. The screenshot above shows the user you are logged in as, the machine you are logged into, and the current directory you are in.

My username is “danielpataki,” the machine I am logged into is “MacBook-Pro.local” and my location is “~”. The tilde character is a short way to say “the current user’s home directory.” On my Mac this is the same as “/users/danielpataki.”

Listing and Changing Directories

To change directories you can use the cd command, followed by an absolute or relative path. I have a WordPress installation on my computer, the absolute path is “/users/danielpataki/vvv/www/wordpress-default”. Since I am in the home directory I could get there using either of the following commands:

The first command uses the relative path, which is calculated from the current directory. The second command uses the absolute path, which starts from the very top of the file system.

To list the contents of a directory use the ls command. It should produce output similar to the screenshot below:

Listing A Directory
Listing A Directory

Note: hidden files will not be shown when listing the directory like this. You’ll need to add the -a option to the command to include them: ls -a

Hidden Files
Hidden Files

The Anatomy of a Terminal Command

When I first started using the terminal, I was always confused about all the little additions commands use, such as options, arguments, flags, operators, switches, oh my… I later found out that the confusion is well–founded – there is no governing body behind naming these things.

Let’s simply map out what variations commands can have and you’ll see things much more clearly.

I’ve created a faux command which contains most elements you’ll see in commands.

The name of the command is “loremipsum,” the idea is that it would spit out some filler text for you. I’ve used two flags. “-p” is supposed to wrap paragraphs in p tags. “-h” is supposed to create headings using heading tags.

The command also has a single argument: “http://fillerama.io/,” the source of the lorem ipsum text. I’ve added a parameter: “–length=5”, which should generate five paragraphs.

COURSES

WP Academy – WordPress Wisdom, Bitesized.

Structured e-learning to expand your WordPress and general business horizons. Learn from people like our very own CEO James Farmer with more than a decade’s experience running a WordPress Business!.

TRY WPMU DEV FREE LEARN MORE

SSH and Accessing Servers

Remember at the beginning of tutorial when we looked at the information displayed in the terminal? Two bits of that information included a username and the machine we were accessing. SSH is a mechanism through which you can log in to remote machines as a specific user and work with the data within.

Ask your web host for SSH details if you don’t have them on hand. You will need a server, a port, a username and a password. If your host does not support SSH access you’ll need to install WordPress locally. Take a look at our guide to using Vagrant for help on how to get that done.

The command for accessing your server will be one of the following:

Use the first command if you didn’t receive a port number. This means SSH uses the default port. Use the second command if you received a port number.

Finally, if you’re running WordPress locally, run the final command from the directory of your virtual machine.

Windows users, you will need an application named PuTTY to get started with SSH. MediaTemple has a guide available if you need help.

Final Words

The command line gives you access to a multitude of tools. Each one is unique and used for different purposes but by understanding the structure of a command and becoming a little but familiar with the terminal you’ll be able to use them with a lot more ease.

In upcoming tutorials in this series, we’ll take a look at some utilities that use the command line heavily to give you some more experience and some hands-on knowledge of useful tools.

Daniel Pataki
Did you find this tutorial helpful? Why do you want to learn WordPress development? What do you want to know more about? Let us know in the comments below.