Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WP CLI: An Intro and Interesting Use Cases

WP CLI: An Intro and Interesting Use Cases

WP-CLI is the command line interface for WordPress. In this talk, I introduce the core concepts of WP-CLI, talk about how to use it to manage your websites, and cover a few interesting use cases for it.

If you’ve never heard of WP-CLI, or you’re an experienced user, hopefully there will be something of interest for you in this talk.

John Blackbourn

April 26, 2018
Tweet

More Decks by John Blackbourn

Other Decks in Technology

Transcript

  1. @johnbillion #WPLDN

    View Slide

  2. View Slide

  3. Interfaces to WordPress
    • Admin area
    • Ajax
    • REST API
    • XML-RPC
    • RSS feeds
    • Themes
    • Comments

    View Slide

  4. WP-CLI is a command line interface for WordPress.
    The project’s goal is to offer
    a complete alternative to the WordPress admin.
    For any action you might want to perform
    in the WordPress admin, there should be
    an equivalent WP-CLI command.

    View Slide

  5. wp-cli.org

    View Slide

  6. $

    View Slide

  7. Built-in User Manual
    wp help
    wp help
    wp --help
    Guided Interactive Mode
    wp --prompt
    How to Get Help

    View Slide

  8. Online Handbook
    wp-cli.org → Handbook
    Online Command Reference
    wp-cli.org → Commands
    How to Get Help

    View Slide

  9. Generally follows "noun [noun]... verb" format
    wp user create
    wp post meta add
    wp plugin install
    But not always...
    wp import
    Command Structure

    View Slide

  10. One-off usage in the terminal:
    How to use
    $ wp user create john [email protected] --role=editor
    Success: Created user 6.
    $ wp plugin list
    +-------------------------+----------+--------+---------+
    | name | status | update | version |
    +-------------------------+----------+--------+---------+
    | airplane-mode | active | none | 0.2.4 |
    | email-log | inactive | none | 2.2.5 |
    | query-monitor | active | none | 3.0.0 |
    | revisions-digest | inactive | none | 0.1.0 |
    | rewrite-rules-inspector | inactive | none | 1.2.1 |
    | user-switching | active | none | 1.3.1 |
    | wordpress-importer | active | none | 0.6.4 |
    | wp-crontrol | inactive | none | 1.7 |
    | db.php | dropin | none | |
    | object-cache.php | dropin | none | |
    +-------------------------+----------+--------+---------+

    View Slide

  11. For scripting purposes:
    How to use
    #!/usr/bin/env bash
    # Configure the script to exit immediately if any command fails:
    set -e
    # Download WordPress core files if they're not already present:
    if [ ! -f "wp-load.php" ]; then
    wp core download
    else
    echo "WordPress files are already present."
    fi
    # Create a wp-config.php file if it's not already present:
    if [ ! -f "wp-config.php" ]; then
    echo "Please enter your database credentials:"
    wp config create --prompt
    fi

    View Slide

  12. "A complete alternative
    to the WordPress admin"

    View Slide

  13. $ wp post list
    $ wp post create --post_title="Hello World"
    $ wp post edit
    $ wp term list category
    $ wp term list post_tag

    View Slide

  14. $ wp post list --post_type=attachment
    $ wp media import https://example.com/image.png
    $ wp media regenerate

    View Slide

  15. $ wp post list --post_type=page
    $ wp post create --post_type=page
    --post_title="About"

    View Slide

  16. $ wp comment list
    $ wp comment approve
    $ wp comment spam

    View Slide

  17. $ wp theme list
    $ wp theme install wpldn --activate
    $ wp theme mod set colour-scheme light
    $ wp widget move widget-4 --position=2
    $ wp menu delete all-pages
    $ wp theme --edit // nope

    View Slide

  18. $ wp plugin list
    $ wp plugin activate wordpress-seo
    $ wp plugin search debug-bar

    View Slide

  19. $ wp user list
    $ wp user list --search=john
    $ wp user list --role=editor
    $ wp user create emily [email protected]
    $ wp user update john --user_password=hunter2

    View Slide

  20. $ wp option get blogname
    $ wp option update admin_email [email protected]
    $ wp option list --search="thumbnail_*"
    $ wp rewrite structure '%post_id%'

    View Slide

  21. Why Use WP-CLI?

    View Slide

  22. WP-CLI is Fast
    1. Navigate to admin area
    2. Enter username
    3. Enter password
    4. Click on Plugins
    5. Click Add New
    6. Search for plugin name
    7. Find plugin in results
    8. Click "Install"...
    9. Click "Activate"...
    $ wp plugin install
    wordpress-seo --activate

    View Slide

  23. WP-CLI is Efficient
    $ wp user update $(wp user list --search="*example.com"
    --search-fields=email --format=ids) --role=subscriber
    Success: Updated user 6.
    Success: Updated user 8.
    Success: Updated user 9.
    Success: Updated user 7.
    Success: Updated user 10.
    Success: Updated user 15.
    Success: Updated user 18.
    Success: Updated user 27.
    Success: Updated user 34.
    Success: Updated user 35.
    Success: Updated user 37.
    Success: Updated user .....

    View Slide

  24. Prevent users from using the installers/editors/updaters:
    define( 'DISALLOW_FILE_MODS', true );
    or
    wp config set DISALLOW_FILE_MODS true --type=constant --raw
    WP-CLI remains unaffected; allows for controlled
    management:
    wp core language install it_IT
    wp plugin update --all
    wp theme install twentyeighteen
    Enhance Your Security

    View Slide

  25. Skip loading all or some plugins or themes:
    --skip-plugins
    --skip-plugins=a-slow-plugin
    --skip-themes
    Load PHP file before running the command:
    --require=fixer.php
    Show all PHP errors:
    --debug
    Administer broken or slow sites

    View Slide

  26. What do you do repeatedly on
    your site that you ought to be
    able to automate?
    Interlude

    View Slide

  27. Aliases avoid the need to SSH into remote sites:
    wp @prod theme activate twenty seventeen
    wp @dev user create dan [email protected]
    wp @all core check-update
    Config:
    @trunk:
    path: /sites/wp/src
    @blog:
    ssh: [email protected]~/var/www/jb
    @dev:
    ssh: vagrant:default
    Administering remote sites

    View Slide

  28. Convert a site to HTTPS
    $ wp search-replace http://example.com https://example.com
    +------------------+-----------------------+--------------+------+
    | Table | Column | Replacements | Type |
    +------------------+-----------------------+--------------+------+
    | wp_commentmeta | meta_key | 0 | SQL |
    | wp_commentmeta | meta_value | 7 | SQL |
    | wp_comments | comment_author | 0 | SQL |
    | wp_comments | comment_author_email | 0 | SQL |
    | wp_options | option_name | 0 | SQL |
    | wp_options | option_value | 12 | PHP |
    | wp_options | autoload | 0 | SQL |
    | wp_postmeta | meta_key | 0 | SQL |
    | wp_postmeta | meta_value | 135 | PHP |
    ...
    Success: Made 271 replacements.

    View Slide

  29. Quickly connect to MySQL
    $ wp db cli
    MariaDB [trunk.wp]> show fields from wp_term_taxonomy;
    +------------------+---------------------+------+-----+---------+
    | Field | Type | Null | Key | Default |
    +------------------+---------------------+------+-----+---------+
    | term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL |
    | term_id | bigint(20) unsigned | NO | MUL | 0 |
    | taxonomy | varchar(32) | NO | MUL | |
    | description | longtext | NO | | NULL |
    | parent | bigint(20) unsigned | NO | | 0 |
    | count | bigint(20) | NO | | 0 |
    +------------------+---------------------+------+-----+---------+
    6 rows in set (0.00 sec)

    View Slide

  30. Blog via the CLI
    $ wp post create --post_title="Hello World" --edit
    // Opens your configured editor
    $ wp post edit 45

    View Slide

  31. Shell Friends
    $ wp post delete $(wp post list--format=ids)
    Success: Trashed post 64.
    Success: Trashed post 186.
    $ wp language core list --field=language | xargs -n 1 wp language
    core install
    Downloading translation from https://wordpress.org/.../af.zip...
    Success: Language installed.
    Downloading translation from https://wordpress.org/.../as.zip...
    Success: Language installed.
    Downloading translation from https://wordpress.org/.../az.zip...
    Success: Language installed.
    Downloading translation from https://wordpress.org/.../bel.zip...
    Success: Language installed.
    Downloading translation from https://wordpress.org/.../bg.zip...
    Success: Language installed.
    ...

    View Slide

  32. wp-cli.org
    wp-cli.tips

    View Slide

  33. Q&A
    @johnbillion #WPLDN
    wp-cli.org wp-cli.tips

    View Slide