Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Siteground webinar - Learn How WP-CLI Can Make Your Life Easier

Siteground webinar - Learn How WP-CLI Can Make Your Life Easier

Webinar description

WP-CLI, the command-line interface for WordPress, is one of the most popular tools among WordPress developers. However, it can be extremely helpful for anyone managing a WordPress site. In this webinar, the main person behind WP-CLI project: Alain Schlesser will show us the ins and outs of the tool. He will give us great examples on how it can be used to improve your workflow, for beginners to experienced developers and for projects big and small. We will give you invaluable examples on how to perform different tasks with WP-CLI automatically, saving you time and preventing you from making manual errors, like:

  • Keep forgetting to backup? Automate the process with WP-CLI.
  • Site not working? Get tips for easy troubleshoot with WP-CLI.
  • Too many spam comments? Learn how to clean them in no time.
  • Can’t log into your site? Reset your password with WP-CLI.
  • Need to change the name of your site? WP-CLI makes it easy.

Links

Script

1. Introduction

My name is Alain Schlesser, I'm a freelance software engineer and the maintainer of WP-CLI.

You can find my blog at https://www.alainschlesser.com and follow me on Twitter at @schlessera.

2. Installing WP-CLI

We're installing WP-CLI in our home folder's bin subfolder. This subfolder needs to be in the PATH environment variable for WP-CLI to be accessible.

cd ~/bin
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
mv wp-cli.phar wp
chmod +x wp
wp cli info

Updating it later on:

wp cli update

Getting help:

wp help <command> <subcommand>

Or:

wp <command> <subcommand> --help

The second notation is more convenient because you always just append it when you started typing a command and are unsure what the final parameters are. The first notation requires help to be the first word after wp.

More info: https://make.wordpress.org/cli/handbook/installing/

3. Server access

Note: This server will be deactivated after the webinar. Use SSH login settings for your own server here.

Web:

http://hristop13.sg-host.com/

SSH:

ssh u126-wjdeyjeuu9mh@gnldm1001.siteground.biz -p18765

Doing quick checks:

wp core version
wp plugin list
wp theme list

4. Creating a new admin user

wp user create --prompt

Note: We're immediately changing the password again through a script so that webinar attendees cannot grab control of the server: sgw-modify-password <user-id>

5. Creating an alias

Add @sgw-prod alias:

wp cli alias add @sgw-prod --set-ssh=u126-wjdeyjeuu9mh@gnldm1001.siteground.biz:18765/home/u126-wjdeyjeuu9mh/www/hristop13.sg-host.com/public_html

Confirm:

wp cli alias list

Usage:

wp @sgw-prod <command>

Adding an even more convenient shell alias:

alias sgw="wp @sgw-prod"

Note: For this shell alias to be persistent, you'll need to add it to your shell profile files. Otherwise, it will be gone again after you log out of your shell.

Adding alias groups to manage multiple servers:

wp cli alias add @allsgw --grouping=sgw-prod,sgw-stage

6. Fix a broken site

Skip loading plugins to enable WP-CLI functionality:

wp plugin list --skip-plugins[=<plugin-slug>]

Deactivate faulty plugin:

wp plugin deactivate <faulty plugin slug> --skip-plugins

7. Reset a user's password

Send a "Reset your password" email to a given user:

wp user reset-password <id|login>

8. Changing the URL for a site

Checkking the current site URL:

wp option get siteurl

Backup database first:

wp db export

Do a dry run first to see what will be changed:

wp search-replace "http://hristop13.sg-host.com" "https://hristop13.sg-host.com" --skip-column=guid --dry-run

Omit the --dry-run to actually persist these suggested changed to the database.

Roll back the database in case something went wrong:

wp db import <name of SQL file that was generated>

9. Automate backups

When done from within the remote site:

wp db export

Executing on remote from local and pulling the backup to local:

scp -P 18765 u126-wjdeyjeuu9mh@gnldm1001.siteground.biz:/home/u126-wjdeyjeuu9mh/www/hristop13.sg-host.com/public_html/"$(wp @sgw-prod db export --porcelain)" ~/backups

Example cron entry:

0 0 1 * * /bin/sh /Users/alain/bin/run_backups.sh >/dev/null 2>&1

10. Clean spam

Generating spam for testing:

wp comment generate --format=ids --count=3 | xargs -d ' ' -I % wp comment spam %

Cleaning all spam comments:

wp comment delete $(wp comment list --status=spam)

11. Creating scripts

Example script:

#!/bin/bash
wp db optimize
wp db export daily-backup.sql

Moving the script to the home folder bin subdirectory and making it executable:

mkdir ~/bin
mv samplescript ~/bin
chmod +x ~/bin/samplescript

Note: Make sure your ~/bin folder is found in the environment's $PATH variable.

12. Working with multisite

Manage network sites:

wp site ...

Manage network meta:

wp network meta ...

Convert or install mutlisites:

wp core multisite-convert ...
wp core multisite-install ...

Run a command against a specific subsite:

wp --url=subdomain.example.com <command>

13. Profiling performance issues

Install additional profiling package first:

wp package install wp-cli/profile-command

Profile all stages:

wp profile stage --all --spotlight

Profile a specific hook:

wp profile hook init --spotlight

14. Siteground integrations

Discovering specific Siteground commands:

wp sg --help

Flushing the cache:

wp sg purge

Enabling GZIP compression:

wp sg optimize gzip enable

15. Deleting expired transients

wp transient delete --expired

16. Managing the object cache

Checking the current setup:

wp cache type

Flushing the object cache:

wp cache flush

17. Scaffolding a Gutenberg block

wp scaffold block my-new-block ...

18. Disabling options autoloading

wp option update <key> --autoload=no
@dreed12

This comment has been minimized.

Copy link

commented Jul 19, 2019

When I run the wp cli alias add command, I get the following response:

Error: Config file does not exist:

Anyone know why this might be happening? I do have a wp_config.php file and other commands work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.