WordPress —Command Line Tool WP-CLI

Lemon Kazi
Oceanize Lab Geeks
Published in
4 min readDec 5, 2018

Probably the WordPress is one of the most used CMS and blogging platform on the web and also it’s probably the most criticized one in the PHP community as well, because the way it works but not sure about that so pardon me if I’m wrong.

Anyways, so whatever it’s but it has dominated the web for a long period of time and like this one, no other CMS or application (or whatever you call it) could made so far. It’s still very active and serving pretty well.

One of the biggest advantages is that, it’s plug-n-play, I mean, for every single need there is a plugin available and even non-programmers (advanced uses) can easily install it and can run the system for their online shipping sites with a very less effort because plenty of themes and plugins are just only one click a way.

If you know how to search and where to search for a theme or plugin then you can start an online shopping site without writing any code.

But, time has been changed and our expectations are being higher. Now, it’s time for modern programming techniques and tools like composer, bower, behat, codeception and so many cool things that become one of the part of our daily development process and we can’t think anything without OOP. In this case, WordPress doesn’t meet the needs. Well, probably it does but we just think it like this, such as it’s all about messy templating with a mash up of HTML and PHP code, which sounds a boring task but probably there are better ways to use WordPress using modern programming tools and techniques. Well, actually we can write clean code with WordPress using modern testing tools like behat and dependency manager like composer and we may integrate templating engines like twig and blade as well to write clean code for building a WordPress theme.

There is already a plugin (could be more) called timber which helps to build a template using twig templating engine which cleans-up your theme code so your PHP file can focus on supplying the data and logic, while your twig file can focus completely on the presentation. You may read more abouttimber.

Anyways, one of the most amazing things that really a cool addition into the WordPress is it’s Command Line Tool which is not very new but not even very old and it’s WP-Cli.

WP-CLI is a set of command-line tools for managing WordPress installations. You can update plugins, set up multisite installs and much more, without using a web browser.

This tool is really one of the sexiest things in WordPress and it’s very helpful for developers, it lets download and install WordPress from command line and also we can use it to manage the site very easily and not only this but also we may generate code snippets to speed up our development process. It’s very easy to install, just visit the home page and read the instructions. Notice there are alternative install methods available and it can be done using composer as well. Okay, enough talk, now let see some examples, I’ve installed it using composer by running following command from my terminal:

> composer create-project wp-cli/wp-cli — no-dev

It’s possible to install it globally, to do that, just create a folder and use cd folder to get into that folder then run the above command so it’ll be installed in that directory and make sure to add the executable path in your path (for windows) or move it to the bin directory for nix base OS. Once it’s installed then check if it works by running wp from your command line. It’ll show some information including it’s current version number. Now lets download WordPress from command line using WP-Cli, run following commands step by step:

wp core download

Then create a database (I’m using mysql from command prompt):

# Log into mysql (Make sure your mysql executable is accessible from your current folder)

> mysql -u root -pyourpassword;

# create a new database

> create database wpcli_test;

#Exit out from mysql

> exit;

Then run the following command to provide log in credentials:

# Provide your login credentials

> wp core config — dbname=wpcli_test — dbuser=root — dbpass=yourpassword

Then run the following command to start installation:

# Provide following options properly

# url, title, admin_user, admin_password, admin_email

> wp core install — url=http://localhost/your-project-directory — title=SiteTitle — admin_user=wplogin — admin_password=wppassword

If everything want right then WordPress will be installed within a few seconds. That’s it, without using the mouse, just from command line the WordPress has been installed very easily. Well, that’s not all but now we’ll see some more examples that we can run from command prompt using WP-Cli but there are many commands and I won’t cover all so visit the WP-Cli website to check fill list of commands.

WP-CLI has a series of global parameters which work with all commands. They can be specified either as flags for the wp executable in the terminal, or defined in a YAML config file.

It’s possible to create a post from command line using following command:

# Create a Post

> wp post create — post_type=post — post_title=”A post” — post_status=publish

To update a post use update command:

# Update the Post whose id is 5; set post_status=publish

> wp post update 5 — post_status=publish

--

--