Daniele Scasciafratte - WordPress Meetup Roma
#wproma

WP-CLI

What you can do with this tool?

Daniele Scasciafratte / @Mte90Net

Daniele Scasciafratte

  • Co Founder/CTO Codeat
  • Open Source Addicted
  • Mozillian & Mozilla Reps & Mozilla TechSpeaker
  • WordPress Core Contributor/Developer
  • Project Translation Editor in WordPress Italia
  • Industria Italiana Software Libero Vice Presidente

Few info

PHP 5.3+

WordPress 3.7 or later

No root privileges required

SSH support (make.wordpress.org/cli/handbook/running-commands-remotely/)

Official project since December 2016

Already included on VVV

Extendable by plugin or packages

No login required

FAST

How to install it


curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
###### OR #######
composer global require wp-cli/wp-cli
###### OR #######
Deb/RPM builds: https://github.com/wp-cli/builds
					

Update it


wp cli update
					

By make.wordpress.org/cli/handbook/installing/ with Bash/ZSH completions docs

Install WordPress


wp core download --locale=it_IT
wp core config --dbuser="wp" --dbpass="wp" --dbprefix="wp_"
wp core install --siteurl="https://yourdomain.tld"
##### OR #####
wp core install --prompt
					

By developer.wordpress.org/cli/commands/core/install/

Real examples!

Change a constant on wp-config.php

wp config set WP_DEBUG true --type=constant --raw

Load PHP file before running the command

--require=fixer.php

Import image by url

wp media import url --post_id=123 --title="It's me an image"

Search & Replace


wp search-replace http://domain.com https://domain.com
wp search-replace http://domain.com https://domain.com --network
##### OR #####
wp search-replace http://olddomain.com https://newdomain.com --export=newdb.sql --all-tables
					

Upgrade all the websites in a folder


#!/bin/bash
declare -a wp_sites=('/usr/local/web/site1/htdocs' '/usr/local/web/site2/htdocs' '/usr/local/web/site3/htdocs')

for site in "${wp_sites[@]}"; do
    echo Upgrading $site...
    wp --path=$site db export $site/db-backup.sql
    wp --path=$site core update
done
					

Disable commands for a site with `wp-cli.yml`


disabled_commands:
    - db drop
    - search-replace
					

Database operation


wp db query "show tables;"
wp db export
wp db reset --yes # Yes reset the DB!
wp db import ~/db-export.sql
wp db search 'http:\/\/' --regex --stats
wp db size --tables
					

Plugin management


wp plugin install glossary-by-codeat --activate
##### OR #####
wp plugin activate glossary-by-codeat
wp plugin deactivate glossary-by-codeat
wp plugin update glossary-by-codeat
wp plugin list
					

Options


wp option get option-name
wp option add option-name
wp option delete option-name
					

Various tasks


wp transient delete --expired
wp cache flush # Redis Memcached or PHP-APCu
wp media regenerate 
vim $(wp config path)
					

Profile package


wp package install wp-cli/profile-command
wp profile stage --allow-root --all
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| hook                     | callback_count | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| muplugins_loaded:before  |                | 0.178s  | 0.0009s    | 1           | 25%         | 1          | 3            | 0s           | 0             |
| muplugins_loaded         | 2              | 0.0003s | 0s         | 0           | 50%         | 1          | 1            | 0s           | 0             |
| plugins_loaded:before    |                | 0.2855s | 0.0041s    | 19          | 83.95%      | 136        | 26           | 0s           | 0             |
| plugins_loaded           | 34             | 0.2442s | 0.0009s    | 3           | 98.31%      | 116        | 2            | 0s           | 0             |
| setup_theme:before       |                | 0.0005s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| setup_theme              | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| after_setup_theme:before |                | 0.2653s | 0.0011s    | 3           | 99.68%      | 615        | 2            | 0s           | 0             |
| after_setup_theme        | 17             | 0.0241s | 0.0002s    | 1           | 98.8%       | 82         | 1            | 0s           | 0             |
| init:before              |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| init                     | 82             | 0.261s  | 0.0016s    | 9           | 98.74%      | 703        | 9            | 0s           | 0             |
| wp_loaded:before         |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_loaded                | 7              | 0.0012s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| parse_request:before     |                | 0.0192s | 0s         | 0           | 100%        | 14         | 0            | 0s           | 0             |
| parse_request            | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| send_headers:before      |                | 0.0001s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| send_headers             | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| pre_get_posts:before     |                | 0.0003s | 0s         | 0           | 100%        | 10         | 0            | 0s           | 0             |
| pre_get_posts            | 8              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts:before         |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts                | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp:before                |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp                       | 10             | 0.068s  | 0.0148s    | 30          | 97.3%       | 756        | 21           | 0s           | 0             |
| template_redirect:before |                | 0.0005s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect        | 20             | 0.0098s | 0.0005s    | 1           | 99.06%      | 105        | 1            | 0s           | 0             |
| template_include:before  |                | 0.0004s | 0s         | 0           | 100%        | 9          | 0            | 0s           | 0             |
| template_include         | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_head:before           |                | 0.0028s | 0.0003s    | 2           | 85.71%      | 18         | 3            | 0s           | 0             |
| wp_head                  | 42             | 0.0887s | 0.004s     | 9           | 97.85%      | 682        | 15           | 0s           | 0             |
| loop_start:before        |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_footer                | 14             | 0.0068s | 0s         | 0           | 100%        | 61         | 0            | 0s           | 0             |
| wp_footer:after          |                | 0.0002s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (31)               | 242            | 1.4571s | 0.0284s    | 78          | 90.8%       | 3323       | 84           | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
wp profile hook --all --spotlight --url=https://yourdomain.com --allow-root
+----------------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| callback                                     | location                                            | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+----------------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| wpseo_init()                                 | wordpress-seo/wp-seo-main.php:252                   | 0.0136s | 0s         | 0           | 100%        | 34         | 0            | 0s           | 0             |
| wpto_Public->wpto_remove_yoast_information() | wp-theme-optimizer/public/class-wpto-public.php:187 | 0.0151s | 0s         | 0           | 100%        | 18         | 0            | 0s           | 0             |
| wp_enqueue_scripts()                         | wp-includes/script-loader.php:1294                  | 0.015s  | 0s         | 0           | 100%        | 348        | 0            | 0s           | 0             |
| generate_construct_header()                  | generatepress/inc/template-tags.php:514             | 0.0041s | 0.0002s    | 2           | 100%        | 93         | 0            | 0s           | 0             |
| generate_add_navigation_after_header()       | generatepress/inc/navigation.php:23                 | 0.0132s | 0.0003s    | 3           | 100%        | 367        | 0            | 0s           | 0             |
| wp_trim_excerpt()                            | wp-includes/formatting.php:3289                     | 0.0186s | 0s         | 0           | 100%        | 16         | 0            | 0s           | 0             |
| generate_construct_sidebars()                | generatepress/functions.php:434                     | 0.0129s | 0.0001s    | 1           | 100%        | 178        | 0            | 0s           | 0             |
+----------------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (7)                                    |                                                     | 0.0927s | 0.0006s    | 6           | 100%        | 1054       | 0            | 0s           | 0             |
+----------------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
					

Profile every plugin


#!/bin/bash
echo "Loop of curl request, with plugin disabled for the page $1"

for p in $(wp plugin list --fields=name --status=active)
do
echo "Plugin: $p"
    for i in {1..5}
    do
        curl -so /dev/null -H "Pragma: no-cache" -w "%{time_total}\n" $1 | sed 's/\,/./'
    done | awk '{ sum +=$1; n++; print $1 } END { if (n > 0) print "AVG: " sum / n; }'
    wp plugin activate $p
done
					

Profile a page


#!/bin/bash

echo "Loop of curl request for the page $1"

for i in {1..20}
do
    curl -so /dev/null -H "Pragma: no-cache" -w "%{time_total}\n" $1 | sed 's/\,/./'
done | awk '{ sum +=$1; n++; print $1 } END { if (n > 0) print "AVG: " sum / n; }'
					

Create a command


# Register a custom 'foo' command to output a supplied positional param.
#
# $ wp foo bar --append=qux
# Success: bar qux

/**
 * My awesome closure command
 *
 * Message
 * : An awesome message to display
 *
 * --append="your message"
 * : An awesome message to append to the original message.
 *
 * @when before_wp_load
 */
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	$foo = function( $args, $assoc_args ) {
		WP_CLI::success( $args[0] . ' ' . $assoc_args['append'] );
	};
	WP_CLI::add_command( 'foo', $foo );
}
					

Cool package to discover for you

https://github.com/markri/wp-sec

https://github.com/mikedance/wp-cli-favorite-plugins

https://github.com/petenelson/wp-cli-size

https://github.com/wp-cli/doctor-command

WP CLI GUI

wpcligui.com

THE END

- https://wp-cli.org/