Call to undefined function __() in wp-db.php

2019-09-13 00:14发布

问题:

I have a standalone script in my WordPress installation and I have used the following to load the wpdb object:

define( 'SHORTINIT', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

However, my error log outputs the following when I call the script:

PHP Fatal error:  Call to undefined function __() in /wp-includes/wp-db.php on line 1297

When I go to line 1297 in wp-db.php, I see

_doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );

If I comment out that line, the script doesn't crash, but being a core WordPress file, I don't think that's the best solution.

I do use the prepare method in a separate class, however it is only at the standalone script file that I get this error. When I re-use that same separate class by creating an object in a file that WordPress "recognizes", I don't get the error.

回答1:

According to WP docs, __ is located in wp-includes/l10n.php - so you could try and include that as well (and hope that that then does not itself refer to stuff that in turn needs other files ;-)


Apparently it does work, so I'll just add it as an answer.



标签: php wordpress