0

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?

Thanks

4
  • Better to use WP Rest API. ;-) Commented 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 Commented 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? Commented Mar 15, 2017 at 20:38
  • 1
    What are you trying to do that the $wpdb object will not work? Commented Mar 15, 2017 at 21:09

2 Answers 2

0

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.)

2
  • 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 Commented 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. Commented Mar 17, 2017 at 15:54
-1

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

require_once(ABSPATH . '/wp-settings.php'); 
1
  • 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 Commented Mar 16, 2017 at 11:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.