29
A better WordPress workflow with WP-CLI By Rikesh Ramlochund

A Better WordPress Workflow with WP-CLI

Embed Size (px)

Citation preview

Page 1: A Better WordPress Workflow with WP-CLI

A better WordPress workflow with WP-CLIBy Rikesh Ramlochund

Page 2: A Better WordPress Workflow with WP-CLI

WP-CLI

› WP-CLI is a set of command-line tools for managing WordPress installations

› Install a WordPress website

› Update WordPress core, plugins, and themes

› Automate repetitive tasks

› Make your own commands

› ssh on your server and run the commands

Page 3: A Better WordPress Workflow with WP-CLI

WP-CLI Installation

› Installation and upgrade: https://wp-cli.org/

› Download the wp-cli.phar filecurl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

› Check if the file worksphp wp-cli.phar —-info

Page 4: A Better WordPress Workflow with WP-CLI

WP-CLI Installation

› To be able to type just wp, instead of php wp-cli.phar, you need to make the file executable and move it to somewhere in your PATH.

› chmod +x wp-cli.phar

› sudo mv wp-cli.phar /usr/local/bin/wp

› wp —-info

Page 5: A Better WordPress Workflow with WP-CLI

WP-CLI Installation

› WP-CLI can also be installed via Composer or Homebrew

› Debian and Ubuntu users can install WP-CLI via a .deb package

› WP-CLI can also be installed on Windows

› Read more about WP-CLI Installation: https://wp-cli.org/docs/installing/

Page 6: A Better WordPress Workflow with WP-CLI

WP-CLI Command Anatomy› wp plugin install jetpack › wp: All WP-CLI commands start with wp

› plugin: command

› install: subcommand

› jetpack: options/parameters (here it is a plugin name)

Page 7: A Better WordPress Workflow with WP-CLI

Node.js Wrapper

› npm install wp-cliNode wrapper for Wordpress CLI with functionality matching the WP-CLI API.Learn more: https://www.npmjs.com/package/wp-cli

Page 8: A Better WordPress Workflow with WP-CLI

Installing WordPress using WP-CLI

› Download the WordPress core:wp core downloadorwp core download —-locale=fr_FR

Page 9: A Better WordPress Workflow with WP-CLI

Installing WordPress using WP-CLI

› Generate the wp-config.php filewp core config --dbname=devconwp --dbuser=devcon --dbpass=somePassword

› Create the database file (dbname value from command above)wp db create

Page 10: A Better WordPress Workflow with WP-CLI

Installing WordPress using WP-CLI

› Run the WordPress installation processwp core install —-prompt

› Installation done :)

› Note: ——prompt is a global parameter of WP-CLI, and it prompts the user to enter values for all command arguments

Page 11: A Better WordPress Workflow with WP-CLI

Installing WordPress using WP-CLI

› Recap: › wp core download › wp core config --dbname=devconwp --dbuser=devcon —-dbpass=somePassword

› wp db create › wp core install —-prompt

Page 12: A Better WordPress Workflow with WP-CLI

WordPress Core commands

› Check for update via Version Check APIwp core check-update

› Update WordPresswp core update and wp core update-db

› Verify WordPress files against wordpress.org checksumswp core verify-checksums

› Display the WordPress versionwp core version

Page 13: A Better WordPress Workflow with WP-CLI

Plugin Management

› List all pluginswp plugin list

› List plugins with pending updateswp plugin list —-update=available

› Plugin Installationwp plugin install advanced-custom-fieldswp plugin install jetpack —-version=“3.6”wp plugin install my-plugin.zip

Page 14: A Better WordPress Workflow with WP-CLI

Plugin Management

› Install premium plugins via zip file.

› Advanced Custom Fields Pro allows you to download a zip file using your private key

› wget -v -O acf-pro.zip “http://connect.advancedcustomfields.com/index.php?p=pro&a=download&k=YOUR_ACF_PRO_KEY”

› wp plugin install acf-pro.zip --activate › rm acf-pro.zip

Page 15: A Better WordPress Workflow with WP-CLI

Plugin Management

› Some plugins like Jetpack also implement WP-CLI commands.

› For example:wp jetpack statuswp jetpack module listwp jetpack module activate carousel

Page 16: A Better WordPress Workflow with WP-CLI

Plugin Management

› Activate a pluginwp plugin activate jetpack

› Deactivate a pluginwp plugin deactivate jetpack

› Update all pluginswp plugin update --all

Page 17: A Better WordPress Workflow with WP-CLI

WordPress Maintenance

› Keeping WordPress up to date › wp core update › wp core update-db › wp plugins update --all › wp theme update --all › Don’t forget to backup your files before doing updates!

Page 18: A Better WordPress Workflow with WP-CLI

WordPress Backups

› Export WordPress database via WP-CLIwp db export

› Compress the WordPress directorytar -vczf backup.gz .

Page 19: A Better WordPress Workflow with WP-CLI

Database Migration

› Moving a WordPress installation to another server (eg. staging to production)

› Step 1: Export staging databasewp db export

› Step 2: Move database to production and importwp db import

› Step 3: Update URLs from staging to productionwp search-replace “http://staging.example.com” “http://example.com”

Page 20: A Better WordPress Workflow with WP-CLI

Managing Options

› Read option valuewp option get blogname

› Update option valuewp option update blogname “Awesome WP”

› Delete optionwp option delete optional

› List optionswp option list

Page 21: A Better WordPress Workflow with WP-CLI

Useful Commands

› Regenerate thumbnailswp media regenerate

› Generate x posts, using some Lorem Ipsum text curl http://loripsum.net/api/5 | wp post generate --post_content --count=x

› Run a query saved in a filewp db query < debug.sql

› Command helperwp help plugin install

Page 22: A Better WordPress Workflow with WP-CLI

Useful Commands

› Import WXR filewp import posts.xml

› Bulk importing images as attachmentsImport all JPGs from the user’s Pictures directorywp media import ~/Pictures/**\/*.jpg

› Import a specific image and assign it as post thumbnail for that postwp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image

Page 23: A Better WordPress Workflow with WP-CLI

Useful Commands

› Delete all transientswp transient delete-all

› Delete all expired transientswp transient delete-expired

› List registered post typeswp post-type list

Page 24: A Better WordPress Workflow with WP-CLI

Useful Commands

› Update user passwordwp user update 1 —-user_pass=newpass

› List all postswp post list

› List all userswp user list

Page 25: A Better WordPress Workflow with WP-CLI

Scaffolding

› _s Theme scaffoldingwp scaffold _s devconmru --theme_name="The Devconmru Theme" —-author=“Devconmru” —-sassify

› New post typewp scaffold post-type books —-theme

› Create a child themewp scaffold child-theme devconmru-child --parent_theme=devconmru —-theme_name=‘Child Theme'

Page 26: A Better WordPress Workflow with WP-CLI

WP-CLI Internal API

› Extend WP-CLI with new commandWP_CLI::add_command()

› Execute callbacks for a specific hookWP_CLI::do_hook()

› Displaying messagesWP_CLI::line()WP_CLI::success() WP_CLI::error() //script exited after this one

Page 27: A Better WordPress Workflow with WP-CLI

Custom WP-CLI Commands

› Extend WP-CLI with your own commands

› No performance issues since the code is only loaded when accessed through WP-CLIif ( defined('WP_CLI') && WP_CLI ) { require __DIR__ . '/wp-cli-command.php';}

Page 28: A Better WordPress Workflow with WP-CLI

Custom WP-CLI Commands› function post_count_callback(){ //Write code here } WP_CLI::add_command('post-count', 'post_count_callback');

› New command available! wp post-countDemo code: https://github.com/rrikesh/wp-cli-post-count

Page 29: A Better WordPress Workflow with WP-CLI

Thank you!

› Questions?

› If you have any questions after the presentation: Tweet me at @rrikeshMail me on [email protected]