In the function below, I'm trying to convert the fopen/fwrite/fclose methods to the preferred WP_filesystem methods. Everything works as expected using the commented out script block, however, the WP_filesystem methods always return the error block.
Any ideas what I'm doing wrong?
function so_doHtaccess(){
if ( !current_user_can( 'manage_options' ) ) return;
if ( file_exists( get_home_path() . ".htaccess" ) ) {
$htaccess_file = get_home_path() . ".htaccess";
$htaccessnew = get_option('my_updated_htaccess');
/*
if ( is_writeable( $htaccess_file ) ) {
$f = fopen( $htaccess_file, 'w+' );
fwrite( $f, $htaccessnew );
fclose( $f );
}
*/
WP_Filesystem();
global $wp_filesystem;
$f = $wp_filesystem->get_contents($htaccess_file);
if ( ! $wp_filesystem->put_contents( $f, $htaccessnew, 0644) ) {
echo "There was an error saving the htaccess file. As a result no changes were made";die;
}
}
return;
}
When I var_dump($wp_filesystem) it yields:
object(WP_Filesystem_Direct)#358 (4) {
["verbose"]=> bool(false) ["cache"]=> array(0) { }
["method"]=> string(6) "direct"
["errors"]=> object(WP_Error)#361 (2) {
["errors"]=> array(0) { }
["error_data"]=> array(0) { }
} }