Find Wordpress root directory in bash without WP-CLI












2















How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question




















  • 1





    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
    Jan 26 at 0:21













  • Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

    – Slam
    Jan 27 at 4:45













  • This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

    – Slam
    Jan 27 at 4:52
















2















How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question




















  • 1





    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
    Jan 26 at 0:21













  • Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

    – Slam
    Jan 27 at 4:45













  • This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

    – Slam
    Jan 27 at 4:52














2












2








2








How can I find the WordPress root directory in Bash without using WP-CLI?










share|improve this question
















How can I find the WordPress root directory in Bash without using WP-CLI?







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 23:49









Dave Romsey

12.9k83854




12.9k83854










asked Jan 25 at 22:52









SlamSlam

227111




227111








  • 1





    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
    Jan 26 at 0:21













  • Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

    – Slam
    Jan 27 at 4:45













  • This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

    – Slam
    Jan 27 at 4:52














  • 1





    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
    Jan 26 at 0:21













  • Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

    – Slam
    Jan 27 at 4:45













  • This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

    – Slam
    Jan 27 at 4:52








1




1





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
Jan 26 at 0:21







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
Jan 26 at 0:21















Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

– Slam
Jan 27 at 4:45







Sure Tom. My current install uses Docker. There's no MySQL server running in my MacOS and wp does nothing without MySQL running. I could ssh into the docker container and use wp there, but that would only give me paths inside the docker container; for BackstopJS to work in my instance I needed various paths on the host machine and not the Docker paths. Ditto for rsync'ing plugins between local and remote devs (though there are other ways to sync plugins, I know).

– Slam
Jan 27 at 4:45















This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

– Slam
Jan 27 at 4:52





This might also be useful in installs where the hosting provider does not provide or allow wp-cli.

– Slam
Jan 27 at 4:52










1 Answer
1






active

oldest

votes


















2














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




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.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    Jan 26 at 0:21











  • Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

    – Slam
    Jan 27 at 4:46













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
});


}
});














draft saved

draft discarded


















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









2














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




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.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    Jan 26 at 0:21











  • Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

    – Slam
    Jan 27 at 4:46


















2














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




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.






share|improve this answer


























  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    Jan 26 at 0:21











  • Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

    – Slam
    Jan 27 at 4:46
















2












2








2







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




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.






share|improve this answer















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




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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 1 at 12:51









shea

4,59532651




4,59532651










answered Jan 25 at 22:52









SlamSlam

227111




227111













  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    Jan 26 at 0:21











  • Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

    – Slam
    Jan 27 at 4:46





















  • Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

    – Tom J Nowell
    Jan 26 at 0:21











  • Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

    – Slam
    Jan 27 at 4:46



















Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

– Tom J Nowell
Jan 26 at 0:21





Note that the wp-config.php file can be one level up, in which case this will generate the wrong result

– Tom J Nowell
Jan 26 at 0:21













Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

– Slam
Jan 27 at 4:46







Yup, I added to that the Caveats section at the bottom. Is there any other file or folder that is guaranteed to be at wp root? I could search for that instead.

– Slam
Jan 27 at 4:46




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix