How to set path with WP_Filesystem and ftp base / chroot

In my theme i will use the WP_Filesystem to write a file to for example:

$file = get_template_directory().'/library/assets/css/wpless2css.css';

Which outputs something like /home/user/domain/http_docs/wp-contents/theme/mytheme/library/assets/css/wpless2css.css

This will work unless the ftp user will be chroot to /home/user/domain/ (on some servers?).

In the case of a chroot the WP_Filesystem can’t find $file.

A solution seems to define FTP_PATH in wp-config.php into /home/user/domain/ and use:

if(FTP_BASE) $file =  str_replace(FTP_BASE,'',get_template_directory()).'/library/assets/css/wpless2css.css';

I’m looking for a solution more robust and support for the different possible permissions systems.

1 Answer
1

After changing FTP_BASE in my wp-config i found the update function of WP broken. Then i realize the update function work the same way and don’t need any additional settings.
In wp-admin/includes/update-core.php i found the usage of $wp_filesystem->wp_content_dir(). This solves my problem too.

On http://xref.wordpress.org/branches/3.8/WordPress/Filesystem/WP_Filesystem_Base.html you will find a list of possible useful functions:

string wp_content_dir () 
string wp_lang_dir () 
string wp_plugins_dir () 
string wp_themes_dir ([string $theme = false])

In fact @otto give my the answer earlier (Using wp_filesystem in Plugins), here he wrote:

When using the $wp_filesystem, there’s a handy function call for
getting the content directory path: $wp_filesystem->wp_content_dir();.
You need to use this function because the “remote” directory path may
not be the same as the “local” directory path.

Leave a Comment