Converting fopen/fwrite operations to WP_filesystem

The get_contents function returns the contents of the file, not a file handle. Using put_contents with $f after that is incorrect.

Try $wp_filesystem->put_contents( $htaccess_file, $htaccessnew, FS_CHMOD_FILE ) instead.

Also, it’s not necessarily safe to use things like “get_home_path” and such with the WP_Filesystem. The remote path may not match the local path. You’ll need to do something like this:

$homedir = $wp_filesystem->abspath();

To get the equivalent of the ABSPATH on the other filesystem.