3

I am confronted to the error PHP Notice: Undefined index: HTTP_HOST when trying to use wp-cli on my wordpress install because I have this line in my wp-config.php:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');

Bug is reported here https://github.com/wp-cli/wp-cli/issues/730 and there is a solution here https://make.wordpress.org/cli/handbook/common-issues/#php-notice-undefined-index-on-_server-superglobal which is to set a default value in wp-cli context:

if ( defined( 'WP_CLI' ) && WP_CLI && ! isset( $_SERVER['HTTP_HOST'] ) ) {
    $_SERVER['HTTP_HOST'] = 'example.com';
}

I don't understand this solution though. Isn't the point of $_SERVER['HTTP_HOST'] NOT to have to set a value for the current domain name? Or does this example mean that I can put ANY value for $_SERVER['HTTP_HOST'], no matter my real domain name, as it won't be used by wp-cli?

Sulli
  • 763
  • 1
  • 11
  • 33

2 Answers2

3

I had the same configuration and error and the workaround I use to bypass the error is to always add

--url=mysite.com 

at the end of the wp-cli command for a specific site. This sets the required WP_HOME, WP_SITEURL and HTTP_HOST for wp-cli to operate on so you do not need to update wp-config.php file.

For multisite's sitewide command, you may want to use

wp site list --field=url | xargs -n1 -I % wp YOUR-CLI-COMMAND --url=%
Dharman
  • 30,962
  • 25
  • 85
  • 135
Adebowale
  • 29
  • 6
-1

In Wordpress if you don't specify the WP_HOME, WP_SITEURL and the HTTP_HOST you get these warnings. For my website these does not prevent it of working, but I think if its giving you waring its because needed to be addressed.

These lines bellow will get these 3 variables assigned correctly and the warnings will disappear...

if ( defined( 'WP_CLI' ) && WP_CLI ) {
  $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = 'Here you need to add the server name or website name';
}

define( 'WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
define( 'WP_SITEURL', WP_HOME );

Hope these can fix that issue!

O.Caliari
  • 313
  • 2
  • 5
  • This changes nothing. When you provide these answers, do you test them out yourself first? I think you should start doing that. – Aries Aug 06 '22 at 08:45