2

I get the following error after moving to multisite when running my cronjob:

Notice: Undefined index: HTTP_HOST in /var/www/html/myapp/wp-includes/ms-settings.php on line 38

Call Stack:
    0.0009     392912   1. {main}() /var/www/html/myapp/wp-content/themes/fundingpress-child/test.php:0
    0.0010     395888   2. require('/var/www/html/myapp/wp-content/themes/fundingpress-child/bootstrap.php') /var/www/html/myapp/wp-content/themes/fundingpress-child/test.php:6
    0.0065     687056   3. require_once('/var/www/html/myapp/wp-load.php') /var/www/html/myapp/wp-content/themes/fundingpress-child/bootstrap.php:7
    0.0066     699048   4. require_once('/var/www/html/myapp/wp-config.php') /var/www/html/myapp/wp-load.php:37
    0.0074     800592   5. require_once('/var/www/html/myapp/wp-settings.php') /var/www/html/myapp/wp-config.php:89
    0.0329    3665088   6. require('/var/www/html/myapp/wp-includes/ms-settings.php') /var/www/html/myapp/wp-settings.php:103


Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/myapp/wp-includes/ms-settings.php:38) in /var/www/html/myapp/wp-includes/ms-settings.php on line 187

Call Stack:
    0.0009     392912   1. {main}() /var/www/html/myapp/wp-content/themes/fundingpress-child/test.php:0
    0.0010     395888   2. require('/var/www/html/myapp/wp-content/themes/fundingpress-child/bootstrap.php') /var/www/html/myapp/wp-content/themes/fundingpress-child/test.php:6
    0.0065     687056   3. require_once('/var/www/html/myapp/wp-load.php') /var/www/html/myapp/wp-content/themes/fundingpress-child/bootstrap.php:7
    0.0066     699048   4. require_once('/var/www/html/myapp/wp-config.php') /var/www/html/myapp/wp-load.php:37
    0.0074     800592   5. require_once('/var/www/html/myapp/wp-settings.php') /var/www/html/myapp/wp-config.php:89
    0.0329    3665088   6. require('/var/www/html/myapp/wp-includes/ms-settings.php') /var/www/html/myapp/wp-settings.php:103
    0.0346    3804784   7. header() /var/www/html/myapp/wp-includes/ms-settings.php:187

My cronjob php file:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
require_once(  __DIR__ .'/../../../wp-load.php' );
?>
bippo
<?php

$myfile = fopen(__DIR__ . "/xxxxxxxxxxxxxxxxxxxxnewfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

die("bb");

And yes: this cronjob PHP file is just an example. I need access to all WordPress functions. If I remove the wp-load.php call, this works. When my site was not multisite, this works. Now, it doesn't work for said error.

halfer
  • 19,824
  • 17
  • 99
  • 186
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • i dont do much with multisite but it cannot break include/require......by your error load seems to be loading but server http host seems to be undefined, the only thing in your code that can cause this is `__DIR__` which is probably relying on server settings, try a static path to rule it out – David Jun 10 '16 at 22:18
  • 1
    Have you set up Multisite correctly? It can be a bit of a pain, in particular you need to be sure you've got everything right with the new constants in wp-config.php. You should be able to still use cron with Multisite, and including wp-load.php is the way to pull the environment in. Another alternative though might be to use wp_cron - is that a possibility? – Tim Malone Jun 10 '16 at 22:28
  • @david well it does break it. go to `ms-settings.php` if some server variable is not set, it redirects the entire request! – Toskan Jun 11 '16 at 00:39
  • 1
    @TimMalone you will see, `ms-settings.php` tries to access `$_SERVER['HTTP_HOST']` which per definition, is not set from CLI. I actually ended up setting it in case of CLI runs myself (e.g. i set `$_SERVER['HTTP_HOST'] = 'mydomain.com'` and `$_SERVER['REQUEST_URI'] = '/'` before i run the scripts and now it works – Toskan Jun 11 '16 at 00:42
  • @TimMalone i didnt know about `wp_cron`, nice pointer there – Toskan Jun 11 '16 at 00:43

1 Answers1

2
  global $_SERVER;
  $_SERVER['HTTP_HOST'] = 'www.mydomain.com';
  $_SERVER['REQUEST_URI'] = '/';
  require_once(  __DIR__ .'/../../../wp-load.php' );

seems to have fixed my issues where mydomain should be your actual domain (localhost.localdomain probably when using locally or localhost, i used a local virtual host )

Toskan
  • 13,911
  • 14
  • 95
  • 185