Advanced WordPress Management Using WP-CLI on Alibaba Cloud ECS — Part 3

1. Managing Multiple WordPress sites using WP-CLI

It’s now obvious that using a CLI is much more efficient and productive than the browser to manage your WordPress sites. Things work much more quickly, and the commands are really quite easy once you get used to them.

$ cd ~
$ wp @example plugin list
$ wp @another plugin list
# Alias Groups
@all-examples:
- @example
- @another
$ sudo touch /usr/local/bin/wp-plugin-list.sh
$ sudo nano /usr/local/bin/wp-plugin-list.sh
#!/bin/bash 
declare -a sites=(
'/var/www/an-example-domain.com/htdocs'
'/var/www/another-example-domain.com/htdocs'
)
for site in "${sites[@]}";
do
echo “List plugins for:
echo $site
wp --path=$site plugin list
echo “ “
done

2. Using SSH with WP-CLI for remote management

So far so good. We are now using the power of WP-CLI to manage multiple WordPress sites on the same server. If we have dozens or hundreds of sites this will save an incredible amount of valuable time.

$ sudo mkdir ~/.wp-cli && sudo touch ~/.wp-cli/config.yml
$ sudo nano ~/.wp-cli/config.yml
#Global Parameter Defaults
disabled_commands:
- db drop
# Aliases
@example:
ssh: new_user@an-example-domain.com/var/www/an-example-domain.com/htdocs/
@another:
ssh: new_user@another-example-domain.com/var/www/another-example-domain.com/htdocs/
@all-examples:
- @example
- @another
$ cd /local/sites/anotherexampledomain/app/public
$ sudo nano wp-cli.yml
@test:
ssh: new_user@another-example-domain.com/var/www/test.another-example-domain.com/htdocs
@staging:
ssh: new_user@another-domain.com/var/www/staging.another-domain.com/htdocs/
@production:
ssh: new_user@another-domain.com/var/www/another-domain.com/htdocs/
@test:
ssh: new_user@another-example-domain.com/var/www/test.another-example-domain.com/htdocs
@staging:
ssh: new_user@another-domain.com/var/www/staging.another-domain.com/htdocs/
@production:
ssh: new_user@another-domain.com/var/www/another-domain.com/htdocs/
@prerelease:
- @test
- @staging

3. Using WP-CLI Scaffold to speed up development

WP-CLI has a command called ‘wp scaffold’ that is used to generate the boilerplate code necessary for many of the standard WordPress development tasks.

  • Child Themes
  • Feature Plugins
  • Custom Post Types
  • Custom Taxonomies
  • PHPunit testing of plugins
  • PHPunit testing of themes
wp @example scaffold child-theme twentyseventeen-child --parent_theme=twentyseventeen --theme_name="Twenty Seventeen Child" --author="New User" --author_uri="an-example-domain.com" --theme_uri="an-example-theme.com" —activate
<?php
global $wpdb;
$random_example_post = $wpdb->get_var(
"SELECT post_title
FROM $wpdb->posts
WHERE post_type = 'post'
AND post_status = 'publish'
ORDER BY rand()
LIMIT 1"
);
echo "A random example post: $random_example_post \n";
if( defined( 'WP_CLI' ) && WP_CLI ) {        class Custom_Commands {                function random_post ( $args, $assoc_args ) {                        global $wpdb;                        $random_post = $wpdb->get_var(
"SELECT post_title FROM $wpdb->posts
WHERE post_type = 'post'
AND post_status = 'publish'
ORDER BY rand()
LIMIT 1");
WP_CLI::success( "Random post: $random_post" );
}
}
WP_CLI::add_command( 'custom_commands', 'Custom_Commands' );
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com