WordPress WP-CLI PHP Notice: Undefined index: HTTP_HOST

wp-cli logo black and white

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:

macariojames@ip-1-2-3-4:/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'];. With the error message, this is the key part: Undefined index: 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. Be sure to check what exception is being thrown, as that will tell you what you need to change. Another example is receiving

Fatal error: require_once(): Failed opening required file-name-here.php 

you would need to ensure permissions for that file were correct.

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.


(Visited 642 times, 1 visits today)

If you found this post useful ...

Buy Me a Coffee logo
Wondering why you keep seeing lower-cased 'i' in my posts? Read -> Why ‘i’ is not capitalized
0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x