我正在尝试从网络服务接收文件并将其保存到wordpress中,但我有以下问题,
注意:第22行的/Applications/MAMP/htdocs/wordpress/wp-load.php中已经定义了常量ABSPATH
致命错误:在第95行的/Applications/MAMP/htdocs/wordpress/wp-content/plugins/myproject/Myclasses/retrieve_2334.php中调用未定义的函数wp_generate_attachment_metadata()
我的代码:
ini_set( 'display_errors', TRUE );
error_reporting( E_ALL );
require("/Applications/MAMP/htdocs/wordpress/wp-load.php");
$pos = strrpos($Address, "&f=");
$loc = substr($Address, $pos + 3);
$Address = "http://dev.piction.com/v60/" . $Address;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $Address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$upload_dir = wp_upload_dir();
$location= $upload_dir['basedir']. "/" . $loc;
try{
$location= $upload_dir['path']. "/" . $loc;
$wp_filetype = wp_check_filetype($loc, null );
$fullpathfilename = $upload_dir['path'] . "/" . $loc;
$fileSaved = file_put_contents($location, $output);
if ( !$fileSaved ) {
throw new Exception("The file cannot be saved.");
}
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $loc),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $upload_dir['url'] . "/" . $loc
);
$attach_id = wp_insert_attachment( $attachment, $fullpathfilename, 0 );
if ( !$attach_id ) {
throw new Exception("Failed to save record into database.");
}
//require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullpathfilename );
wp_update_attachment_metadata( $attach_id, $attach_data );
} catch (Exception $e) {
$error = '<div id="message" class="error"><p>' . $e->getMessage() . '</p></div>';
}
echo 'Photo is uploaded';
答案 0 :(得分:0)
看起来好像你要包含两次文件,这可以解释为什么你会收到“已经定义”的错误。
尝试删除代码第三行中的require()
语句,看看它是否有效,因为好像该文件已经包含在脚本中的其他位置。
答案 1 :(得分:0)
尝试更改为require_once("/Applications/MAMP/htdocs/wordpress/wp-load.php");
,因为您已经定义了通知,可能表明您已经包含了wp-load.php。
您还需要取消注释行//require ( ABSPATH . 'wp-admin/includes/image.php' );
才能使用wp_generate_attachment_metadata()