我用这个函数把它修好了 request_filesystem_credentials(), 它似乎可以初始化事物并获得适当的凭据。
在本文的一个示例中找到了它:An Introduction to the WordPress Filesystem API.
我的代码:
# get credentials
function connect_fs()
{
global $wp_filesystem;
if( false === ($credentials = request_filesystem_credentials(\'\')) )
{
return false;
}
//check if credentials are correct or not.
if(!WP_Filesystem($credentials))
{
request_filesystem_credentials(\'\');
return false;
}
return true;
}
function donload_images($image_title, $image_url)
{
global $wp_filesystem;
#path to save to
$files_path = ABSPATH . \'/image\';
$save_file_to = $files_path .\'/\'. $image_title;
#check if requested file is on server
$file_exists = file_exists($save_file_to);
if(connect_fs()){
// Get file
if(!$file_exists)
{
#Download file info
$file_to_save = file_get_contents($image_url);
//success
if($wp_filesystem->put_contents($save_file_to, $file_to_save, FS_CHMOD_FILE)){
}
else //Fail
{
}
return $file_to_save;
}
else //File exists
{
}
}
}