menu

Questions & Answers

Require wp-load.php breaks website - possible Multisite issue

I'm trying to write some code to access the WordPress database. Everything tells me to load wp-load.php to do this. However I just get critical errors when I do. This used to work a couple of months back, not sure when it stopped working, so might be a recent WordPress update that has stopped this working. Currently on WordPress 6.1.1

<?php

ini_set( 'display_errors', '1' );
ini_set( 'display_startup_errors', '1' );
error_reporting( E_ALL );

global $_SERVER;
$_SERVER['HTTP_HOST']   = 'www.mydomain.com';
$_SERVER['REQUEST_URI'] = '/';

$file_require = $_SERVER["DOCUMENT_ROOT"] . '/wp-load.php';
include $file_require;

if (file_exists( $file_require )) {
    echo 'FILE EXISTS: file_require: ' . $file_require . '<br>';
} else {
    echo 'FILE NOT FOUND: file_require: ' . $file_require . '<br>';
}

echo 'Successfully Loaded wp-load.php<br>';

The code after the include nevers gets executed. Just get a "There has been a critical error on this website."

The site is a MULTISITE and some articles and similar questions do suggest that this may be teh cuase. But this used to work just fine and this has always been a multisite. As I say, this has stopped working during the last few weeks.

Any help would be gratefully received.

Comments:
2023-01-07 20:29:24
WordPress overrides PHP's error settings. I'd recommend turning WP_DEBUG on in wp-config.php in order to see the specific error message. In the long run, however, I'd recommend using an officially supported method of interacting with WordPress, either a custom WP CLI command, or a plugin. For the latter, you just need to register "something" that is specific to your code, WordPress will boot normally and then call your code. You'll have access to 100% of WordPress features and objects, but you can also just do regular PHP stuff, too.
Answers(0) :