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

1. Managing Multiple WordPress sites using WP-CLI

$ 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

$ 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 @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