Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global option (--config-path) to specify location of WordPress config file #1218

Closed
samstoller opened this issue May 22, 2014 · 13 comments
Closed

Comments

@samstoller
Copy link

My wp-config.php file is located in a non-standard directory levels above wp-content. WordPress is able to locate this file as the standard wp-config.php has been changed to "require()" the actual conf file.

wp-cli is unable to parse my config file as it's just reading the source looking for the constants and wp-setting. So rather than try to parse the PHP code and read the file from the correct location, it would be easier and more flexible to specify a path to the conf file instead.

I'd be happy to work on this and submit a pull request. Thoughts?

@danielbachhuber
Copy link
Member

Why not use the --require attribute to load the config file with the data, and have the normal wp-config load WordPress?

@samstoller
Copy link
Author

Good thinking. Almost worked. Ran into path issues with the wp-settings.php file (ie. it's not being skipped and throws a "No such file" warning).

@danielbachhuber
Copy link
Member

@samstoller mind sharing your setup in a Gist or two (generically, of course, as to not exposing any secrets) so I can play around with it locally?

@samstoller
Copy link
Author

Sure thing.

The reason for these changes is I've installed WordPress as a git submodule. This requires a bit of modification to how things are normally set up. I will share with you my install process, which may seem a bit convulted at first but it's really quite simple. I also use a custom shell script to deploy so I've automated the entire process. If you search for installing WP as a submodule you will find plenty of developers doing the same thing so it's not such an unsual way of going about installing WP these days as it has many benefits.

Here's a simplified version of my install script: https://gist.github.com/samstoller/3f080f65d4126fe5b9c5

Once you've got the correct layout, try using wp-cli and you will find that it struggles with the config file path. To get around this for now, I forked the wp-cli repo, made some hacky changes to the core so it looks in my custom conf/ dir and compiled the tool and everything seems to work pretty well. This lead me to thinking a config path option would work.

@danielbachhuber
Copy link
Member

@samstoller apologies for the late reply here.

When you attempt to run WP-CLI with your configuration, do you get this error?

Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

From the example you've provided, it appears you might be able to use your existing pattern as long as the existing wp-config.php file retains the reference to loading wp-settings.php (ref). If you can trick WP-CLI into thinking it's a valid wp-config.php, it will evaluate and pull out the constants.

Related #1362

@samstoller
Copy link
Author

Yes, that's the error I receive.

So I tried the idea of tricking WP-CLI into thinking it has found the line with wp-settings.php. The regex prevents anything but spaces before the require statement which made the solution a bit evasive at first, but I worked it out to:

/*
require-wp-settings.php
*/

So I ran a command and got a 'Cannot redeclare' type error and realized that when WP-CLI parses the actual config file it doesn't skip the call to wp-settings.php on the second time round thereby including wp-settings.php when it's not supposed to.

The same issue happened when I tried the --require attribute. Essentially there's no way to skip the loading of wp-settings.php.

Ideally, it would be great to see locate_wp_config() (ref) consult a setting before searching other file paths.

@danielbachhuber
Copy link
Member

You'll need to remove require_once ... wp-settings.php from your wp-config.php entirely, as WP-CLI has a custom wp-settings-cli.php: http://wp-cli.org/blog/how-wp-cli-loads-wordpress.html

Ideally, it would be great to see locate_wp_config() (ref) consult a setting before searching other file paths.

Probably not going to happen.

@orev
Copy link

orev commented Nov 13, 2014

This is causing issues for me as well, as I have my wp-config in /etc/wordpress and I'm using a stub wp-config to load it from there using require_once.

I see that this is closed, but I don't think it's uncommon to want to move the wp-config outside of the web directory.

@kevin-coyle
Copy link

Yeah I'm having the same issue as this. I really would like to keep the wp-config outside of a directory that apache can read to.

@credwards27
Copy link

@orev @kevin-coyle

As @danielbachhuber said, the stub wp-config.php file needs to have the following line in it for WP-CLI to parse it correctly:

require_once( ABSPATH . 'wp-settings.php' );

Remove that line from your custom wp-config.php (outside web root), and place it after the require_once statement in your wp-config.php stub. As long as the stub is in the same directory as the rest of Wordpress core, WP-CLI shouldn't have any issues.

@samstoller
Copy link
Author

Exactly right. I made a gist for this for anyone still having trouble. HTH.

https://gist.github.com/samstoller/efc38eb309e62df392ea

@gilzow
Copy link

gilzow commented Feb 13, 2019

I know this closed but add another +1 to the request to allow us to define the location of wp-config.php via parameter.

bwaltz pushed a commit to ColbyCommunications/admissions-site that referenced this issue Sep 12, 2019
@kupietools
Copy link

kupietools commented Dec 30, 2023

Another +1. That gist is not helpful at all. I'm not using any sort of a "stub" but if it can't find my wp-config file for some reason, despite being in the same folder I'm running it from, right there alongside my other WP folders, it would be helpful if I could just tell it where it is with a parameter. Without that or a clear set of instructions for what is needed, it is not usable at all. If I could just do something like "wp --help" or "wp -h" to see what the "--require" option you mentioned is, that would be great, but without it being able to find the wp-config file that's sitting right here in this directory that I'm trying to run it from, even that doesn't work. There really should be an easy way to tell it where the file is, since it's that important.

EDIT: Problem solved on my own. It took some digging, but for anyone coming here looking for a solution to the same issue, the solution was simple for me: manually adding the line require_once(ABSPATH . 'wp-settings.php'); to the end of my wp-config.php file appears to have gotten wp-cli working without giving the Error: Strange wp-config.php file: wp-settings.php is not loaded directly. error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants