我已经解决了我的问题,下面是我的wordpress插件的最终代码。 1-输入Excel进行wordpress的交(包含文章标题和内容描述图像名) 2-图像文件夹
HTML表单后数据导入的wordpress
Upload the xlsx file to Import data.
Please choose a file:
PHP代码文件:
if(isset($_FILES['xl_file']))
{
//upload file to read data from it
$attach_src=$_FILES['xl_file']['tmp_name'];
$file_name=time(). $_FILES["xl_file"]["name"];
$file_name=str_replace(" ","",$file_name);
$upload = wp_upload_bits($file_name, null, file_get_contents($_FILES["xl_file"]["tmp_name"]));
include_once(plugin_dir_path(__FILE__).'PHPExcel/IOFactory.php');
$inputFileName =$upload['file'];
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
echo ('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
//array of post data
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
//divide the post array into chunks (for larger arrays)
$listing=array_chunk($sheetData, 50);
$list=array();
$images_list=array();
//Reading images
$dir = plugin_dir_path(__FILE__)."Images/";
//get all the images list
$images_list = glob($dir."*.*",GLOB_NOSORT);
apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT);
foreach($sheetData as $list_data){
$list=array();
// get post data into $list array
foreach($list_data as $list_rec){
$list[]=$list_rec;
}
//$list = single post array
$listing_information = array(
'post_title' => addslashes(wp_strip_all_tags($list[0])),
'post_content' => addslashes($list[1]),
'post_type' => 'post',
'post_status' => 'publish'
);
$listingid =wp_insert_post($listing_information);
//listingid= post ID
//image name that we will found/search in image folder
$listing_image_name=trim($list[2]);
//listing_image_name
$upload_image=array();
//loop through images array to find the required image
foreach($images_list as $img){
//$img = /home/public_html/my_site/wp-content/uploads/2014/06/image_name.jpg
// want to extract only image name from $img
$image_name = explode('/', $img);
$image_name = $image_name[count($image_name)-1];
$needle = '.';
$listing_image_name_dir = substr($image_name, 0, strpos($image_name, $needle));
//match the image name if yes then insert
if($listing_image_name == $listing_image_name_dir){
//uploading the image to wordpress
$upload_image = wp_upload_bits($images_list[0], null, file_get_contents($images_list[0]));
// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($upload_image['file']), null);
$attachment = array(
'guid' => $upload_image['file'],
'post_mime_type' => $filetype['type'],
'post_title' => $listing_image_name,
'post_content' => '',
'post_status' => 'inherit'
);
//insert wordpress attachment of uploaded image to get attachmen ID
$attach_id = wp_insert_attachment($attachment, $upload_image['file']);
//generate attachment thumbnail
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $upload_image['file']));
//set this attachment as post featured image
//listingid= post ID
set_post_thumbnail($listingid, $attach_id);
break;
}//end of if
}//end of foreach
unset($list);
unset($listing_image);
unset($upload_image);
flush();
}
}