0

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.

PHPDev
  • 152
  • 2
  • 4
  • 19
  • So you are including one random wordpress file and none of the other 20 or so files that wordpress automatically includes and wondering why you are getting an error saying undefined function? – Jonathan Kuhn Jan 20 '17 at 22:04
  • When you say _"standalone script"_, do you mean that it doesn't go through WP, but is accessed directly? In that case (if you want to use some WP file), you either need to [initialize WP](http://wordpress.stackexchange.com/a/47059) in the start of your script (recommended since there otherwise might come more issues later), or simply create a mock of that function yourself.. – M. Eriksson Jan 20 '17 at 22:05
  • 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 ;-) – CBroe Jan 20 '17 at 22:15
  • @JonathanKuhn I don't need to include all other 20 or so files because wp-load.php loads the required ones as outlined in the top 2 answers of this question http://stackoverflow.com/questions/5306612/using-wpdb-in-standalone-script – PHPDev Jan 20 '17 at 22:16
  • @CBroe Thanks, that worked! – PHPDev Jan 20 '17 at 22:18
  • @MagnusEriksson Yes, it's accessed directly. I did try the initialization approach, but it didn't work. Fortunately including l10n.php after wp-load.php solved my issue. – PHPDev Jan 20 '17 at 22:26

1 Answers1

0

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.

CBroe
  • 91,630
  • 14
  • 92
  • 150