Is it possible to include a file using PHP but NOT the other files that it includes

I want to read some values from an include file, so I thought the easiest way is to include the file then all the variables are available to me. Problem is the file includes many other files and many of them include other files - and so on. Just hoping there may be a simple solution…

Short answer, no.

Perhaps try incude_once and see if that works.

Not going to work.

OP, it sounds like you have a very poor architecture on your hands. You might want to consider refactoring your code.

@benanamen - yea not actually the case, what I want to do, just for clarity, is to extract the database credentials from the Wordpress wp-config.php file. The simplest route would be to just include the file and access the variables directly. Problem is the wp-config.php goes on to include MULTIPLE other files.

If you have access to the wp-config.php file then globally replace all includes with a include_once is worth a try although refactoring would be better as @benanamen mentioned.

So can’t you include just the files you need in the script you are working on, instead of including the wp-config?
Or make your own DB access include.

@SamA74 Unfortunately no - because some of the main variables I want to examine are in wp-config
@John_Betong include_once is not an option since it still includes them (once) refactoring is also not an option as I cannot refactor the Wordpress files

I think the answer to my question is basically - no.

Since an ideal simple solution is not available and I cannot amend the source Wordpress files I will investigate extracting the wp-config.php line by line, identifying the variables I want and saving them for processing. So I won’t actually be including the wp-config at all.

But thanks to all again for your help

Try setting a flag variable before including wp-config.php, get the variables then test for the flag and exit because the flag is set. On return make the flag null.

Not tried but I think it should work ok.

@John_Betong - I think you are misunderstanding. I can include the wp-config once that’s no problem the problem is wp-config includes multiple other files that I don’t want. Even if I just include wp-config once I still get all the other code from all the other files. It is a case of wanting to include wp-config but NOT the files wp-config includes. I cannot amend the wp-config file because that would break the Wordpress system. But thanks for your ideas.

OK I have accepted this is not possible, so I am pursuing another route - to extract the variables I want from wp-config without including it.

I have the following script to do this but there are some issues with single / double quotes which I am exploring in a separate post How do I format preg_match() to identify single quote or double quote when matching for those who want to follow.

Script so far to extract Wordpress database credentials - works with vars enclosed in single quotes but not double quotes

<?php
$file_name = 'wp-config.php';
$value = file_get_contents($file_name);

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

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

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

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

preg_match('/define.*DB_CHARSET.*\'(.*)\'/', $value, $m);
$dbcharset = $m[1];

preg_match('/define.*DB_COLLATE.*\'(.*)\'/', $value, $m);
$dbcollate = $m[1];

echo $dbname, $dbuser, $dbpass, $dbhost, $dbcharset, $dbcollate;
?>

Don’t do this. You would have to run this parsing on every request which would get very expensive. Just make a file with your db credentials and accept that you will need to keep it in sync. No need to overthink things.

@ahundiak - I do not need to run it multiple times -

I just need to run a script that automatically retrieves this info - I do not want to manually inspect the file and type it in. The whole idea is to -

but automatically

OK, I have found a work around that basically does what I wanted - to include a php file but not any files it includes -

In this case it is actually require_once rather than include but will work for any statement I want to ignore and not execute. I simply open wp-config and replace the string ‘require_once’ with ‘//require_once’ and save as a new file. Then I can include the new file and the statements are commented out.

I don’t have to alter the original file
I can include the new file and have access to all variables
All includes, requires or any operations can be ignored

Thanks for everyone’s help and comments, they basically encouraged me to look at things from a different angle :smile: Code below in case it helps others, could be used for many other reasons…

<?php
$content = file_get_contents("wp-config.php");
$newcontent = str_replace("require_once", "//require_once", "$content");
file_put_contents("temp-config.php", "$newcontent");
?>

Lets back up a minute. Tell us about the real problem you are trying to solve instead of asking about your attempted solution to it. What is the high level overview of what you have going on?

@benanamen - I really do appreciate your help and others but in this instance and for this post my problem is simply as the title states. It appears there is no php built in function or simple command to do this. Therefore the nearest I can get to this is the code I provided. This achieves the same result by creating a duplicate config.php with subsequent includes / requires commented out. In this way I can include a php file without any ‘sub includes’ - that’s exactly what I wanted to achieve. Again thanks to all :smile:

Your “solution” is no solution at all. Every time you call that file you just repeatedly re-write what is already there. What is the point of that?

The simplest way to do this hack is to just manually create a second DB config file and include/require that as needed, but even that is just a hack to solving the real problem whatever that is. (See XY Problem)

Unless you are doing something completely outside of WordPress, the WP connection object is already created and available.

Thread continues here: How do I format preg_match() to identify single quote or double quote when matching