What exactly does $wp_filesystem->abspath() return?

If you check out the source of abspath():

public function abspath() {
    $folder = $this->find_folder(ABSPATH);
    // Perhaps the FTP folder is rooted at the WordPress install, Check for wp-includes folder in root, Could have some false positives, but rare.
    if ( ! $folder && $this->is_dir( "https://wordpress.stackexchange.com/" . WPINC ) )
        $folder="https://wordpress.stackexchange.com/";
    return $folder;
}

.. you’ll see it’s main purpose is to get the “calculated” path for the filesystem method. For example, with FTP, the FTP account’s root path might be deeper/further down the actual document root. Other methods might also not work with just ABSPATH, hence the find_folder() call (which is actually a wrapper for search_for_folder() which does the real work).

Hence why you’ve seen something like:

$plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), MY_PLUGIN_DIR);

…since MY_PLUGIN_DIR will have been constructed (at some point) based on ABSPATH, but within the context of the filesystem API that path may be invalid – so we replace ABSPATH with the calculated abspath() before writing.

Leave a Comment