I have a few multisites, each with several sites, that I want to auto-generate @aliases for. Is there way to sync those sites as aliases in my site list?
I’d like to be able to target a single site without having to specify --url=http://domain.com/sub-site/
and using the last part of the url seems like a viable name for the alias.
$ wp site list
+---------+----------------------------------------+---------------------+---------------------+
| blog_id | url | last_updated | registered |
+---------+----------------------------------------+---------------------+---------------------+
| 1 | http://domain.com/sub-site/ | 0000-00-00 00:00:00 | 2020-01-00 04:20:00 |
+---------+----------------------------------------+---------------------+---------------------+
$ wp cli alias
---
@all: Run command against every registered alias.
@sub-site:
url: http://domain.com/sub-site/
path: /www/wordpress
$ wp @sub-site theme list
---
...
I can only find the following commands that may help;
Shows current aliases:
$ wp cli alias
Shows all sites
$ wp site list –field=url
(Ideal) Auto generate aliases:
$ wp site aliases
$ wp cli alias –generate
Whatever the solution, it should include the path:
to WordPress for that site and (optional) be able to produce unique aliases that have not already been added to ~/.wp-cli/config.yml
.
WIP
home=$(wp eval "echo get_home_path();"); url=$(wp eval "echo site_url();"); for site in $(wp site list --field=url | sort); do alias_name=$(sed "s,/,,g" <<< $(sed "s,$url,,g" <<< "$site")); if [ ! -z "$alias_name" ]; then echo -e "@$alias_name:\n url: $site\n path: $home\n"; fi; done;
or
path=$(wp eval "echo get_home_path();"); url=$(wp eval "echo site_url();"); wp site list --field=url | sort | cut -d "https://wordpress.stackexchange.com/" -f4 | xargs -I '{}' echo -e "@{}:\n url: $url/{}\n path: $path\n"
| sort | sed -r "s#($url)##g" | cut -d "https://wordpress.stackexchange.com/" -f2 | xargs
| sort | tail -n+2 | cut -d "https://wordpress.stackexchange.com/" -f 4 | xargs
| sort | cut -d "https://wordpress.stackexchange.com/" -f4 | xargs
@site-a:
url: http://domain.com/site-a/
path: /www/wordpress/
@site-b:
url: http://domain.com/site-b/
path: /www/wordpress/
Next step would be to, validate the alias against existing aliases. And include sorting by site path
Sort:
sites=$(wp site list --field=url); readarray -t sorted < <(for a in "${sites[@]}"; do echo "$a"; done | sort); for a in "${sorted[@]}"; do echo "$a"; done
Sort:
wp site list --field=url | sort
Existing alias:
wp cli alias | grep '^@' | grep ':$' | cut -d':' -f3
Remote Alias Generator (run on server)
ssh=user@127.0.0.1;cd /www/wordpress/;home=$(wp eval "echo get_home_path();"); url=$(wp eval "echo site_url();"); for site in $(wp site list --field=url); do alias_name=$(sed "s,/,,g" <<< $(sed "s,$url,,g" <<< "$site")); if [ ! -z "$alias_name" ]; then echo -e "@prefix-$alias_name:\n url: $site\n ssh: $ssh$home\n"; fi; done;