[SOLVED/FIXED]: WordPress wp-cli PHP Notice: Undefined index: HTTP_HOST

no alt text set

I manage a ton of websites. Some servers house several separate installations, which makes it cumbersome to have to login through the web to manage each site. (No multi-site setups.)

I decided last weekend after attending my third WordCamp ATL (#WCATL), i’d start to actually utilize wp-cli on a daily basis now that it’s matured and powerful. Heck, i even thought about writing an ebook on using wp-cli. Stay tuned.

Anyway, to the issue at hand. After installing wp-cli on the server and navigating to a site’s installation directory, i ran wp core check-update and this was the output:

[email protected]:/var/www/somesite.com/htdocs/wp$ wp core check-update
PHP Notice:  Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1169) : eval()'d code on line 29
+---------+-------------+-----------------------------------------------------------+
| version | update_type | package_url                                               |
+---------+-------------+-----------------------------------------------------------+
| 5.2     | major       | https://downloads.wordpress.org/release/wordpress-5.2.zip |
+---------+-------------+-----------------------------------------------------------+

As you can see, even though it threw a PHP Notice, the command still let me know there was an update available. Hmmm. Why is this notice showing? And how do i fix it? Time to scour the forums and docs for some answers. After maybe 45 minutes, i was able to piece together an answer that worked.

Essentially the problem was my custom functions in wp-config.php that called for $_SERVER['HTTP_HOST'];. By default, wp-cli doesn’t know what to do with it. So in order to fix this issue, add this to your code in wp-config.php:

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

Voila. The PHP Notice should no longer pop up when running wp core check-update or any other wp-cli command.

Please let me know in the comments or email if you notice this — or any other — fix stops working so i can update the post.

Cheers.

Wondering why you keep seeing lowercased 'i' in my posts? Read -> Why ‘i’ is not capitalized

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of