Im using the following to access tables information from my Wordpress DB

    require_once(ABSPATH . 'wp-config.php');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($conn, DB_NAME);

It works fine I'm able to connect and display the information. The trouble is when I try to do the same thing from a plugin within the Dashboard under tools, I get the following message:

Notice: Use of undefined constant ABSPATH - assumed 'ABSPATH' in C:\xampp\htdocs\wordpressexpensereport\wp-content\plugins\editdb\connection.php on line 2

Warning: require_once(ABSPATHwp-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wordpressexpensereport\wp-content\plugins\editdb\connection.php on line 2

Fatal error: require_once(): Failed opening required 'ABSPATHwp-config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\wordpressexpensereport\wp-content\plugins\editdb\connection.php on line 2

I have checked my wp config and I do have the following

    /** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Is there something I am missing with my implementation?


  • Better to use WP Rest API. ;-) Mar 15, 2017 at 19:41
  • Is there another solution that does not include WP Rest? From my understanding what i've illustrated should work. I think Mar 15, 2017 at 20:14
  • Where exactly in your plugin do you use require_once(ABSPATH . 'wp-config.php'); ? Could it be that it's too early in the process, and wp-config.php has not loaded yet?
    – nibnut
    Mar 15, 2017 at 20:38
  • 1
    What are you trying to do that the $wpdb object will not work?
    – Cedon
    Mar 15, 2017 at 21:09

2 Answers 2


I'm guessing this is a custom "plugin" where you are wanting to run a file externally that connects to the WordPress database, not really as a standard plugin which loads within WordPress environment, as in that case as @belinus says you can just use $wpdb class.

Anyway, since ABSPATH is defined IN wp-config.php, you can't use ABSPATH to find and load wp-config.php - because it hasn't loaded and thus isn't defined yet. That's the flawed logic... hence the errors you are getting.

If you know for certain that wp-config.php is in a directory above where the "plugin" file is, you can do something like this:

function find_require($file,$folder=null) {
    if ($folder === null) {$folder = dirname(__FILE__);}
    $path = $folder.'/'.$file;
    if (file_exists($path)) {require($path); return $folder;}
    else {
        $upfolder = find_require($file,dirname($folder));
        if ($upfolder != '') {return $upfolder;}
$configpath = find_require('wp-config.php');

This will recursively search for wp-config.php above the plugin file directory and load it when it does. (Again this assumes the wp-config.php is located in a directory above the plugin file.)

  • Thank you for your response @majick, as well as everyone who contributed. I've run into another issue related to this same plugin, not sure what prototcall is, continue on this thread or start a new one? Thanks Mar 16, 2017 at 15:18
  • I think it really depends if it extends upon the existing topic (ie. path loading issues) or is unrelated, in which case I'd suggest post a new question.
    – majick
    Mar 17, 2017 at 15:54

You are probably just missing the slash in the require_once. Change it to:

require_once(ABSPATH . '/wp-settings.php');
  • ABSPATH should be defined with a trailing slash as can be seen in the code posted which is direct from a standard copy of wp-config.php
    – majick
    Mar 16, 2017 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.