WP-CLI Available on Shared Hosting Environment

Everyone knows that WordPress is the most used Open Source application these days and there are quite a lot of useful tools released for improving the client experience with that application. Recently we have found a tool called “WP-CLI” which will dramatically improve the interaction with WordPress for those geeky users that are familiar and quite comfortable using the command line (ssh) of their WordPress Hosting Accounts.

What is CLI?

As you might have heard CLI stands for Command Line Interface and it is used to execute shell commands towards the operating system of a computer. No matter if you would like to send the commands to your local computer or the web server hosting your website you will have to use a Command Line Interface in order to interact with different services.

How actually WP-CLI works?

WP-CLI, executed in the installation directory of your WordPress website will easily handle most of the common tasks you usually perform over the WordPress web interface. For example:

  1. Managing the WordPress caches
  2. Managing the WordPress cron events and schedules
  3. Managing the WordPress media attachments
  4. Installing WordPress
  5. Installing Plugins
  6. Installing Themes
  7. Updating WordPress

and much more. You can see the full list of useful commands at the official WP-CLI Commands page. By executing each command with its parameters you will be able to easily and most importantly securely fully manage your WordPress website.

Using WP-CLI

With the recent improvements of our Shared Hosting environment you are able to use quite a lot of different features and the one we are reviewing in this post can be used with a simple command:

$ wp command parameters

where “command” should be replaced with one of the supported commands and “parameters” with any of the parameters required by the used command.

Examples

Let’s review a few examples of how WP-CLI behave on our WordPress Hosting package.

Installing WordPress

WordPress installation over the command line might be quite handy if you do not have access to your domain name:

  • Create an empty directory and access it:
    $ mkdir wp-test && cd wp-test
  • Download WordPress and prepare it for installation:
    $ wp core download Downloading WordPress 4.1.1 (en_US)...
    Using cached file '/home/s1intfcomet/.wp-cli/cache/core/en_US-4.1.1.tar.gz'...
    Success: WordPress downloaded.
  • Creating the database – you should be able to create a cPanel database following our cPanel Database tutorial;
  • Creating configs:
    $ wp core config
    --dbname=database_name
    --dbuser=database_username
    --dbpass=Database_password
    --locale=en_EN
    Success: Generated wp-config.php file.
  • Running the installation command:
    $ wp core install
    --url="s1int.fcomet.com/wp-test"
    --title="Your Blog Title"
    --admin_user="admin"
    --admin_password="pass123"
    --admin_email="[email protected]"
    Success: WordPress installed successfully
  • The process is a bit extended due to the generation of the configuration file, however, you will end up with an easy and secure WordPress installation.

Installing Themes

The theme installation process is relatively simple and it involves the following actions:

  1. Listing the currently installed themes
$ wp theme list
+----------------+----------+--------+---------+
| name | status | update | version |
+----------------+----------+--------+---------+
| twentyfifteen | active | none | 1.0 |
| twentyfourteen | inactive | none | 1.3 |
| twentythirteen | inactive | none | 1.4 |
+----------------+----------+--------+---------+
  1. Searching for themes – the search is as the usual WordPress themes search but quicker.
$ wp theme search medical
Success: Showing 4 of 4 themes.
+----------------+----------------+--------+
| name | slug | rating |
+----------------+----------------+--------+
| Medical | medical | 50 |
| Medical Center | medical-center | 0 |
| Medica Lite | medica-lite | 100 |
| Creare Site | creare-site | 100 |
+----------------+----------------+--------+
  1. Installing theme – in order to install the selected theme, you will need the theme “slug” from the searching results.
$ wp theme install medical-center
Installing Medical Center (1.0.5)
Downloading install package from https://downloads.wordpress.org/theme/medical-center.1.0.5.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
  1. Activating theme – Again a theme should be activated using it’s “slug
$ wp theme activate medical-center
Success: Switched to 'Medical Center' theme.

In order to activate a different theme, simply use the theme slug from the list of installed themes.

Installing Plugins

The plugin installation process is the same as the Theme installation process but instead searching for the theme’s name you will need to search for the plugin’s name:

$ wp plugin search clef
Success: Showing 7 of 7 plugins.
+--------------------------------+------------------------+--------+
| name | slug | rating |
+--------------------------------+------------------------+--------+
| Clef Two-Factor Authentication | wpclef | 98 |
| BruteProtect | bruteprotect | 96 |
| Neeed | neeed | 0 |
| VOD Infomaniak | vod-infomaniak | 0 |
| Full UTF-8 | full-utf-8 | 100 |
| Canalblog Importer | canalblog-importer | 0 |
| Agent virtuel ASKOM | agent-virtuel-by-askom | 0 |
+--------------------------------+------------------------+--------+

Once you have found the plugin you would like installed, simply use it’s slug again in order to get it installed and again it’s slug to get it activated:

$ wp plugin install wpclef
Installing Clef Two-Factor Authentication (2.2.9.1)
Downloading install package from https://downloads.wordpress.org/plugin/wpclef.2.2.9.1.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
$ wp plugin activate wpclef
Success: Plugin 'wpclef' activated.

Deactivating a plugin is also a piece of cake and in order to do so, you will need to only change “activate” with “deactivate” in the above plugin activation command.

Writing Posts

WP-CLI allows also the writing of posts directly via the command line. You do not have the text editor, however, it serves well if you would like to have a quick post written.

You will need to create a .txt file with the content of the post you would like to be displayed. Let’s call it wp-cli.txt. Once you are ready you will need to upload the file on your web hosting account and to execute the following command:

$ wp post create ./wp-cli.txt --post_title='wp-cli'
Success: Created post 3.

Every other operation you perform on your posts will require for you to provide a post ID. This can be obtained by listing all the posts:

$ wp post list
+----+--------------+-------------+---------------------+-------------+
| ID | post_title | post_name | post_date | post_status |
+----+--------------+-------------+---------------------+-------------+
| 6 | wp-cli | wp-cli | 2015-04-11 10:52:23 | publish |
| 1 | Hello world! | hello-world | 2015-04-11 09:50:26 | publish |
+----+--------------+-------------+---------------------+-------------+

Now that your post is added, you will need to publish it of course if you would like to. Keep in mind that you will need the post ID.

$ wp post update 3 --post_status=publish
Success: Updated post 3.

Respectively to delete a post you will also need its ID:

$ wp post delete 3 --force
Success: Deleted post 3.

Conclusion

WP-CLI happens to be quite handy as it does not depend on the loading speed of your website due to the fact that the queries to the database all made locally on your web hosting account with us. This is indeed time sparing for the administrators and the developers of WordPress websites. From this point on the feature will inhabit our Shared Hosting environment and all our Shared Hosting users will be able to use it. We will update and maintain it in the future as it is one of the most secure ways of managing your WordPress instances.

Christopher

Christopher has many years of experience leading teams in the fields of Technical support, Server Administration, and Product Development. He mainly works on the backend, helping to create the infrastructure that powers FastComet. He is responsible for flawless migrations and quick and efficient answers to client questions. He also monitors our network status and jumps in to solve time-sensitive issues like DDoS attacks and stops malicious attempts in their tracks. Christopher’s primarily responsible for making sure that our servers purr along, and has worked tirelessly to improve automation at FastComet.