我正在编写一个简单的插件,应该从indesign XML文件和图像文件中导出 - 并从中创建帖子草稿。
我有2个输入。一个用于xml文件,一个用于所有图像:
<input type="file" name="xmlupload1[]" id="xmlupload[]" multiple="">
<input type="file" name="imgsupload1[]" id="imgsupload[]" multiple="">
在XML内部有帖子的内容和与这篇文章相关的图像的img标签,如下所示:
<img href="1_ChrisGardner.jpg" caption="credit: wikipedia"></img>
我想像wordpress一样上传图片,将“caption”属性中的相关标题添加到每个图片中,并将其附加到父帖。
我使用$ _FILES数组和wordpress上传函数进行了很多操作但没有成功......
一些php代码:
$XMLfiles = $_FILES['xmlupload']['tmp_name'];
$post_imgs = $_FILES['imgsupload'];
foreach ($XMLfiles as $xmlfile){ {
//CREATE POST FROM XML:
$xmlstring = file_get_contents($xmlfile);
$xml = new SimpleXMLElement($xmlstring);
$content = '';
$title = $xml->xpath('//h1');
$post_title = $title[0];
$excerpt = $xml->xpath('//h2');
$post_excerpt = $excerpt[0];
$author = $xml->xpath('//author');
$post_author = end($author);
$post_id = create_post($post_title, $post_excerpt);
$imagetags = $xml->xpath('//img');
$img_captions = array();
foreach($imagetags as $imagetag) {
$img_caption = '';
$atts = $imagetag->attributes();
$img_fname = end(explode('/', $image['href']));
$img_caption = $atts['caption'];
$img_captions[$img_fname] = $img_caption;
unset($imagetag);
}
$paragraphs = $xml->xpath('//p');
foreach($paragraphs as $p){
$p->addAttribute('class', 'p1');
}
$stories = $xml->xpath('//Story');
foreach($stories as $story) {
$content = $content.str_replace( '
<Story>', '', str_replace( '</Story>', '', $story->asXML()));
}
update_post($post_id, $post_title, $post_excerpt, $content, $post_author, $issue_number);
//ATTACH UPLOADED IMAGES TO POST:
foreach ($post_imgs['name'] as $key => $value) {
if ($post_imgs['name'][$key]) {
$file = array(
'name' => $post_imgs['name'][$key],
'type' => $post_imgs['type'][$key],
'tmp_name' => $post_imgs['tmp_name'][$key],
'error' => $post_imgs['error'][$key],
'size' => $post_imgs['size'][$key]
);
$img_caption = $img_captions[urlencode($file['name'])];
$new_file = wp_handle_upload($file, array( 'test_form' => false ));
$filename = $new_file['url'];
$parent_post_id = $post_id;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'post_excerpt' => $img_caption
);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$img_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
$img_data = wp_generate_attachment_metadata( $img_id, $filename );
wp_update_attachment_metadata( $img_id, $img_data );
}
}
}
它似乎无法正常工作,也无法创建其他图像尺寸......
请帮忙! 在此先感谢!!