0
我想通过php脚本完全在wordpress之外上传图片到wordpress。我知道我必须将数据插入到哪些表中。但我注意到,如果我通过正常的方式通过wordpress创建各种大小的图像,并保存在数据库中。有没有一种方法可以使用完全在wordpress之外的脚本来模拟图片的上传。将图片上传到wordpress并在db中保存详细信息
我想通过php脚本完全在wordpress之外上传图片到wordpress。我知道我必须将数据插入到哪些表中。但我注意到,如果我通过正常的方式通过wordpress创建各种大小的图像,并保存在数据库中。有没有一种方法可以使用完全在wordpress之外的脚本来模拟图片的上传。将图片上传到wordpress并在db中保存详细信息
是的,这将是这样的:
<?php
header('Response: HTTP/1.1 200 OK');
define('WP_USE_THEMES', false);
require('../../../../wp-load.php');
$uploadedfile = $_FILES['image'];
if($uploadedfile['size'] > 0){
$upload_overrides = array('test_form' => false);
if (! function_exists('wp_handle_upload')) require_once(ABSPATH . 'wp-admin/includes/file.php');
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile) {
$wp_filetype = $movefile['type'];
$filename = $movefile['file'];
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $wp_filetype,
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
die(json_encode(array('type'=>'error', 'text' => 'Ok', 'error' => 0)));
}else{
die(json_encode(array('type'=>'error', 'error' => 1)));
}
}
die(json_encode(array('type'=>'error', 'error' => 2)));
}
?>
请注意,从来就粘贴我的代码,you'll有那么一点点的工作;)
伟大的作品的人,谢谢 – user2327579