Slide 1

Slide 1 text

@johnbillion #WPLDN

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

wp-cli.org

Slide 6

Slide 6 text

$

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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 | | +-------------------------+----------+--------+---------+

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

"A complete alternative to the WordPress admin"

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

$ wp comment list $ wp comment approve $ wp comment spam

Slide 17

Slide 17 text

$ 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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

$ 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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Why Use WP-CLI?

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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 .....

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

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)

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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. ...

Slide 32

Slide 32 text

wp-cli.org wp-cli.tips

Slide 33

Slide 33 text

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