在下面的函数中,我试图将fopen/fwrite/fclose方法转换为首选的WP\\u文件系统方法。使用注释掉的脚本块,一切都能正常工作,但是,WP\\u文件系统方法总是返回错误块。
知道我做错了什么吗?
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;
}
当I var\\u dump($wp\\u filesystem)时,它会产生: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) { }
} }