0

I'm working on a project that involves creating EXTRA tables in the same DB that WP uses.

Before, I was simply just including wp-config.php to get the information, but am now having difficulties using this in parts of the app, that are executed OUTSIDE wordpress.

After posting a question on Wordpress.Stackexchange, one user advised me to use the file() function to get the information from wp-config.php.

I've loked into the file() funciton but cant understand it well.

Can someone just post an example code to get constant vars like this from a php file:

wp-config.php:

define('DB_HOST','host');
define('DB_user','user');
define('DB_pass','password');

I need to get those variables, to work on the DB side of things.

2 Answers 2

1

That's not an elegant solution, but you can use preg_match like this if you really need to use a file reading operation:

$f = '/path/to/file';
$c = file_get_contents($f);

preg_match('/define.*DB_NAME.*\'(.*)\'/', $c, $m);
$dbname = $m[1];

preg_match('/define.*DB_USER.*\'(.*)\'/', $c, $m);
$dbuser = $m[1];

preg_match('/define.*DB_PASSWORD.*\'(.*)\'/', $c, $m);
$dbpass = $m[1];

preg_match('/define.*DB_HOST.*\'(.*)\'/', $c, $m);
$dbhost = $m[1];

echo $dbname, $dbuser, $dbpass, $dbhost
Sign up to request clarification or add additional context in comments.

Comments

1

You should just be able to use php's include() function in your external file. The code in the WordPress config file will then execute, which will use php's define() function, setting those PHP Constant Variables. You can then use them to connect to your database.

1 Comment

No I cant do that. As I stated in the post, that is what I was previously doing. I'm experiencing many issues with that method now So I cant use it. I need another way. Thanks.

Your Answer

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