Hello,

My name is Cristi Burcă.

The Internet calls me scribu.

I work on/with WordPress.

Command Line Learnings
For Make Benefit
Glorious Nation of WordPress

spacer

WordCamp Norway 2013

Command Line Interface?

Haven't those gone extinct since the 90's?!

What is WP-CLI?

What is WP-CLI good for?

Automation

Task: Set up a cron script that periodically updates everything.

#!/bin/bash

cd /path/to/wp/install

wp db export /tmp/backup.sql

wp core update
wp theme update --all
wp plugin update --all

Integration

Task: Access data in a WP install from a Python script.

import subprocess

def get_wp_option(key):
    command = ["wp", "option", "get", key]

    proc = subprocess.Popen(command, stdout=subprocess.PIPE)
    out, err = proc.communicate()

    return out.strip("\n")

print get_wp_option("home")

Long running jobs

Task: Regenerate all thumbnails on a site.

GUI:

  • send AJAX request for each image
  • 405 LOC

CLI:

  • simple foreach loop
  • 117 LOC

Interactive debugging

Task: Fool around.

$ wp shell 
Type "exit" to close session.
wp> get_bloginfo('description')
'Just another WordPress site'
wp> strrev($_)
'etis sserPdroW rehtona tsuJ'
wp>

Great success!

Code generation

Task: Create a plugin that registers a custom post type and a taxonomy.

wp scaffold plugin wcnorge

wp scaffold post-type slides --plugin=wcnorge

wp scaffold taxonomy slide_cat --post_types=slides --plugin=wcnorge

What about X?

WP-CLI is extensible; it will look for commands defined inside WordPress plugins or themes.

All you have to do is make sure WP-CLI is running:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    require __DIR__ . '/my-cli-command.php';
}

More info:

github.com/wp-cli/wp-cli/wiki/Creating-Commands

How did WP-CLI get started?

If you can not able do it from command line, is not worth do it.
DevOps Borat

Thanks!

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.