Find Wordpress root directory in bash without WP-CLI
How can I find the WordPress root directory in Bash without using WP-CLI?
command-line
add a comment |
How can I find the WordPress root directory in Bash without using WP-CLI?
command-line
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as thosecomposer
would install
– Tom J Nowell♦
2 hours ago
add a comment |
How can I find the WordPress root directory in Bash without using WP-CLI?
command-line
How can I find the WordPress root directory in Bash without using WP-CLI?
command-line
command-line
edited 3 hours ago
Dave Romsey
12.8k83653
12.8k83653
asked 4 hours ago
SlamSlam
227110
227110
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as thosecomposer
would install
– Tom J Nowell♦
2 hours ago
add a comment |
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as thosecomposer
would install
– Tom J Nowell♦
2 hours ago
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those
composer
would install– Tom J Nowell♦
2 hours ago
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those
composer
would install– Tom J Nowell♦
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Simple bash script to find your Wordpress root
Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?
while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi
Use cases:
- Custom shell scripts to sync local and remote folders and databases.
- Docker workflows where wp-cli
wp
commands won't work. - Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.
How to use
- put this code at the top of any script like
myscript.sh
. - set permissions with
chmod u+x myscript.sh
. - Use
${wproot}
as a variable in any path.
- Example
echo "Uploads path is ${wproot}/wp-content/uploads"
.
- Example
Caveats
This is a simple script and may not work under all conditions. It will not work if:
- your wp-config.php file is not stored in your Wordpress root
- your wp-config.php file is rename
Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.
Suggestions for improvement are welcome.
Note that thewp-config.php
file can be one level up, in which case this will generate the wrong result
– Tom J Nowell♦
2 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326720%2ffind-wordpress-root-directory-in-bash-without-wp-cli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simple bash script to find your Wordpress root
Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?
while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi
Use cases:
- Custom shell scripts to sync local and remote folders and databases.
- Docker workflows where wp-cli
wp
commands won't work. - Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.
How to use
- put this code at the top of any script like
myscript.sh
. - set permissions with
chmod u+x myscript.sh
. - Use
${wproot}
as a variable in any path.
- Example
echo "Uploads path is ${wproot}/wp-content/uploads"
.
- Example
Caveats
This is a simple script and may not work under all conditions. It will not work if:
- your wp-config.php file is not stored in your Wordpress root
- your wp-config.php file is rename
Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.
Suggestions for improvement are welcome.
Note that thewp-config.php
file can be one level up, in which case this will generate the wrong result
– Tom J Nowell♦
2 hours ago
add a comment |
Simple bash script to find your Wordpress root
Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?
while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi
Use cases:
- Custom shell scripts to sync local and remote folders and databases.
- Docker workflows where wp-cli
wp
commands won't work. - Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.
How to use
- put this code at the top of any script like
myscript.sh
. - set permissions with
chmod u+x myscript.sh
. - Use
${wproot}
as a variable in any path.
- Example
echo "Uploads path is ${wproot}/wp-content/uploads"
.
- Example
Caveats
This is a simple script and may not work under all conditions. It will not work if:
- your wp-config.php file is not stored in your Wordpress root
- your wp-config.php file is rename
Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.
Suggestions for improvement are welcome.
Note that thewp-config.php
file can be one level up, in which case this will generate the wrong result
– Tom J Nowell♦
2 hours ago
add a comment |
Simple bash script to find your Wordpress root
Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?
while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi
Use cases:
- Custom shell scripts to sync local and remote folders and databases.
- Docker workflows where wp-cli
wp
commands won't work. - Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.
How to use
- put this code at the top of any script like
myscript.sh
. - set permissions with
chmod u+x myscript.sh
. - Use
${wproot}
as a variable in any path.
- Example
echo "Uploads path is ${wproot}/wp-content/uploads"
.
- Example
Caveats
This is a simple script and may not work under all conditions. It will not work if:
- your wp-config.php file is not stored in your Wordpress root
- your wp-config.php file is rename
Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.
Suggestions for improvement are welcome.
Simple bash script to find your Wordpress root
Ever need to run a script outside of Wordpress and need to know the Wordpress root directory?
while [ ! -e wp-config.php ]; do
if [ $pwd/ = / ]; then
echo "No Wordpress root found" >&2; exit 1
fi
cd ../
done
if [ -e wp-config.php ]; then
wproot=$(pwd)
fi
Use cases:
- Custom shell scripts to sync local and remote folders and databases.
- Docker workflows where wp-cli
wp
commands won't work. - Webpack/grunt workflows where you might be issuing commands from the theme folder instead of the Wordpress root.
How to use
- put this code at the top of any script like
myscript.sh
. - set permissions with
chmod u+x myscript.sh
. - Use
${wproot}
as a variable in any path.
- Example
echo "Uploads path is ${wproot}/wp-content/uploads"
.
- Example
Caveats
This is a simple script and may not work under all conditions. It will not work if:
- your wp-config.php file is not stored in your Wordpress root
- your wp-config.php file is rename
Admittedly the conditions under which you might need this are pretty rare. I needed it for a visual regression test script that needs to traverse several folders and issue various commands, all without wp-cli or Wordpress functions.
Suggestions for improvement are welcome.
edited 4 hours ago
answered 4 hours ago
SlamSlam
227110
227110
Note that thewp-config.php
file can be one level up, in which case this will generate the wrong result
– Tom J Nowell♦
2 hours ago
add a comment |
Note that thewp-config.php
file can be one level up, in which case this will generate the wrong result
– Tom J Nowell♦
2 hours ago
Note that the
wp-config.php
file can be one level up, in which case this will generate the wrong result– Tom J Nowell♦
2 hours ago
Note that the
wp-config.php
file can be one level up, in which case this will generate the wrong result– Tom J Nowell♦
2 hours ago
add a comment |
Thanks for contributing an answer to WordPress Development Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f326720%2ffind-wordpress-root-directory-in-bash-without-wp-cli%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you provide some context as to why WP CLI is specifically excluded in the question? Also, the WP root directory doesn't make sense as a term once you start doing subdirectory installs such as those
composer
would install– Tom J Nowell♦
2 hours ago