a better wordpress workflow with wp-cli
Post on 15-Apr-2017
524 Views
Preview:
TRANSCRIPT
A better WordPress workflow with WP-CLIBy Rikesh Ramlochund
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
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
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
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/
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)
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
Installing WordPress using WP-CLI
› Download the WordPress core:wp core downloadorwp core download —-locale=fr_FR
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
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
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
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
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
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
Plugin Management
› Some plugins like Jetpack also implement WP-CLI commands.
› For example:wp jetpack statuswp jetpack module listwp jetpack module activate carousel
Plugin Management
› Activate a pluginwp plugin activate jetpack
› Deactivate a pluginwp plugin deactivate jetpack
› Update all pluginswp plugin update --all
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!
WordPress Backups
› Export WordPress database via WP-CLIwp db export
› Compress the WordPress directorytar -vczf backup.gz .
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”
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
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
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
Useful Commands
› Delete all transientswp transient delete-all
› Delete all expired transientswp transient delete-expired
› List registered post typeswp post-type list
Useful Commands
› Update user passwordwp user update 1 —-user_pass=newpass
› List all postswp post list
› List all userswp user list
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'
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
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';}
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
Thank you!
› Questions?
› If you have any questions after the presentation: Tweet me at @rrikeshMail me on r@paperboat.io
top related