Find Wordpress root directory in bash without WP-CLI
How can I find the WordPress root directory in Bash without using WP-CLI?
Topic command-line Wordpress
Category Web
How can I find the WordPress root directory in Bash without using WP-CLI?
Topic command-line Wordpress
Category Web
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
wp
commands won't work.myscript.sh
.chmod u+x myscript.sh
.${wproot}
as a variable in any path.
echo "Uploads path is ${wproot}/wp-content/uploads"
.This is a simple script and may not work under all conditions. It will not work if:
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.
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.