Wp-cli doesn't work on multisite + subfolders

I think (but i’m not sure) i should open this here. This could be a wp-cli related problem, anyway i’m unable to use wp-cli to manage a multisite + subfolders bedrock setup provisioned by Trellis.

vagrant@test /srv/www/test.develop.ment/current $ wp core version
4.3.1

vagrant@test /srv/www/test.develop.ment/current $ wp site list --debug
PHP Notice:  Undefined index: HTTP_HOST in /srv/www/test.develop.ment/current/web/wp/wp-includes/ms-settings.php on line 30
PHP Stack trace:
PHP   1. {main}() /usr/bin/wp:0
PHP   2. include() /usr/bin/wp:4
PHP   3. include() phar:///usr/bin/wp/php/boot-phar.php:5
PHP   4. require() phar:///usr/bin/wp/php/wp-cli.php:26
PHP   5. require() phar:///usr/bin/wp/php/wp-settings-cli.php:114
Error: Site  not found.

My wordpress_sites.yml

wordpress_sites:
  test.develop.ment:
    site_hosts:
      - test.develop.ment
    local_path: ../sites/test # path targeting local Bedrock site directory (relative to Ansible root)
    repo: git@github.com:roots/bedrock.git
    site_install: true
[...cut...]
    multisite:
      enabled: true
      subdomains: false
[...cut...]
    env:
      wp_home: http://test.develop.ment
      wp_siteurl: http://test.develop.ment/wp
      domain_current_site: test.develop.ment

in the test folder i’d placed a standard bedrock repo cloned with these lines added in application.php.

/**
 * Multisite Settings
 */
define('WP_ALLOW_MULTISITE', true);
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false); // Set to false if using subdirectories
define('DOMAIN_CURRENT_SITE', getenv('DOMAIN_CURRENT_SITE'));
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

I think this is relevant https://github.com/wp-cli/wp-cli/issues/2058

You can pass in --url when using wp, or you can edit wp-cli.yml in your Bedrock directory to add url: http://example.dev

Also related: https://github.com/wp-cli/wp-cli/pull/2079

Definitely relevant. Added

url: http://test.develop.ment

to wp-cli.yml and the problem is gone. Thank you.

1 Like

I became quite stuck with this, as I had a wp-cli file that was part of the repo I was using. I wrote these three rather messy functions that do a search and replace for all of the 3 different environments the wp-cli.yml file might be set to.

I created a new env var as well called mutlisite_site_name

- name: Replace live with current {{ site_env.mutlisite_site_name }}
  command: sed -i 's/example.com/{{ site_env.mutlisite_site_name }}/g' wp-cli.yml
  args:
    chdir: "{{ deploy_helper.new_release_path }}/"

- name: Replace staging with current {{ site_env.mutlisite_site_name }}
  command: sed -i 's/staging.example.com/{{ site_env.mutlisite_site_name }}/g' wp-cli.yml
  args:
    chdir: "{{ deploy_helper.new_release_path }}/"

- name: Replace development {{ site_env.mutlisite_site_name }}
  command: sed -i 's/example.dev/{{ site_env.mutlisite_site_name }}/g' wp-cli.yml
  args:
    chdir: "{{ deploy_helper.new_release_path }}/"

Then in roles/deploy/defaults/main.yml I added that script

# Deploy hooks
# For list of hooks and explanation, see https://roots.io/trellis/docs/deploys/#hooks
deploy_build_before:
  - "{{ playbook_dir }}/deploy-hooks/change-wp-cli-domain.yml"
  - "{{ playbook_dir }}/deploy-hooks/build-before.yml"

As wp-cli.yml is part of the repo and setting up remote servers tried to use the url of my previously defined dev server, I searched for another solution.

I removed “url” from wp-cli.yml what caused wp-cli to use the fallback URL “example.com” as my DOMAIN_CURRENT_SITE is set to $_SERVER['HTTP_HOST']. The following fixes that and fallbacks to the .env value…

/**
 * WP-CLI never reaches $_SERVER. Define DOMAIN_CURRENT_SITE as fallback to get wp-cli working
 * @link https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal
 */
if ( defined( 'WP_CLI' ) && WP_CLI && ! isset( $_SERVER['HTTP_HOST'] ) ) {
    $_SERVER['HTTP_HOST'] = env('DOMAIN_CURRENT_SITE');
}

This does not work when setting up WordPress MultiSite via
wp core multisite-install --title="site title" --admin_user="username" --admin_password="password" --admin_email="you@example.com"

and wp-cli fallbacks again to example.com. So the main domain needs to get set via the additional parameter --url="your_domain.com".

I’m wondering if there is a better option to make this working more safe also for other users such as getting url value in wp-cli.yml from the .env file.