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;
1 Answer
LOCAL
For local aliases, it works best to define your path ahead of time in the config.yml. Then keep the variables pretty clean by only specifying the url
to target the site. Sorting the output helps if you have a long list of sites since the default is by blog id (which we’re not using).
wp site list --field=url|sort|xargs -I 'SITE' sh -c 'ALIAS=$(cut -d"https://wordpress.stackexchange.com/" -f4 <<< SITE);if [ ! -z "$ALIAS" ];then echo -e "@$ALIAS:\n url: SITE";fi'
Result
path: /www/wordpress/
@site-a:
url: http://domain.com/site-a/
@site-b:
url: http://domain.com/site-b/
REMOTE
For remote aliases, it’s nicer to prefix them relative to the config.yml. So in this case, remote-
. Also you can tack the WordPress path onto the ssh
property.
HOME=$(wp eval "echo get_home_path();");wp site list --field=url|sort|xargs -I 'SITE' sh -c 'ALIAS=$(cut -d"https://wordpress.stackexchange.com/" -f4 <<< SITE);if [ ! -z "$ALIAS" ];then echo -e "@remote-$ALIAS:\n url: SITE\n ssh=user@127.0.0.1$HOME";fi'
Result:
@remote-site-a:
url: http://domain.com/site-a/
ssh: user@127.0.0.1/www/wordpress/
@remote-site-b:
url: http://domain.com/site-b/
ssh: user@127.0.0.1/www/wordpress/
LIST ALIASES
aliases=$(wp cli alias | cut -d':' -f1 | grep -e "^@" | cut -d':' -f2 | sort);
EXISTING ALIASES
aliases=$(wp cli alias | cut -d':' -f1 | grep -e "^@" | cut -d':' -f2 | sort);
search="@site-a"
if ! grep $search <<< "$aliases"; then echo "NOT FOUND"; fi
BASH REFERENCE
VAR=$(echo "stuff")
– Capture outputHOME=$(wp eval "echo get_home_path();")
– Run PHP and assign to varwp site list --field=url | sort
– Sorts the resulting site list| xargs -I 'SITE'
– For loop from pipe and use SITE as the value variable| xargs -I %
– Pick a symbol that makes sensexargs -I 'ITEM' sh -c 'echo ITEM; echo "2nd";'
– Run multiple bash commandscut -d"https://wordpress.stackexchange.com/" -f4 <<< SITE
– Split from variableALIAS=$(cut -d"https://wordpress.stackexchange.com/" -f4 <<< SITE)
– Split URL on “https://wordpress.stackexchange.com/”, assign the 4th index to varecho -e "\txyz\n\n"
– Allow escaped charactersif [ ! -z "$ALIAS" ];then echo -e "";fi
– Run on non-empty variablewp cli alias | grep '^@' | grep ':$'
– Multiple filters on results arraygrep '^starts-with'
– Line starts with valuegrep 'ends-with$'
– Line ends with valuesed "s,$url,,g" <<< "$site"
– sed string replace with,
separatorsed "s/find/replace/g" <<< "$value"
– sed string replace with/
separator (not good with urls)wp site list --field=url | tail -n+2
– Skip first itemalias_name=$(sed "s,/,,g" <<< $(sed "s,$url,,g" <<< "$site"))
– Multiple string replacementsfor i in $(wp site list --field=url); do echo $i; done;
– Loop through list