在对该功能进行了大量搜索之后,我找到了解决方案。
请检查以下示例。
global $wpdb;
if (isset($_POST[\'submit1\'])) {
//$uri = get_template_directory_uri().\'/xmlupload/\';
$uri = wp_upload_dir();
$target_dir = $uri;
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$filename = basename($_FILES["fileToUpload"]["name"]);
$filetypenew = wp_check_filetype($filename);
$uploadOk = 1;
$FileType = pathinfo($target_file, PATHINFO_EXTENSION);
//$FileType = pathinfo($filetype,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "<br/><br/>Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "<br/><br/>Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($FileType != "xml") {
echo "<br/><br/>Sorry, only XML files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<br/><br/>Sorry, your file was not uploaded.";
//echo "<br/>".$uri;
//echo "<br/>".$target_file;
//echo "<br/>file name is ".$filetypenew." file name is ".$filename;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<br/><br/>The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded successfully!!.";
$xml = simplexml_load_file($target_file,null,LIBXML_NOCDATA);
$counter = 0;
$status = \'\';
$state = \'\';
$suburb = \'\';
$additional_array = \'\';
foreach ($xml->children() as $key => $property) {
// Gather post data.
$post = array(
\'post_title\' => $property->headline,
\'post_content\' => $property->description,
\'post_status\' => \'publish\',
\'post_type\' => \'property\',
\'post_author\' => 1
);
// Insert the post into the database.
$post_id = wp_insert_post( $post );
// Gather agent post data.
$agent_post = array(
\'post_title\' => $property->listingAgent->name,
\'post_status\' => \'publish\',
\'post_type\' => \'houzez_agent\',
\'post_author\' => 1
);
// Insert the post into the database.
$agent_id = wp_insert_post( $agent_post );
if(!term_exists( $key, \'property_type\')){
$cat_id = wp_insert_term( ucfirst($key), \'property_type\', array( \'slug\' => $key ) );
wp_set_post_terms( $post_id, array($cat_id[\'term_id\']), \'property_type\' );
}else{
$term = get_term_by(\'slug\', $key, \'property_type\');
wp_set_post_terms( $post_id, array($term->term_id), \'property_type\' );
}
if(!term_exists( strtolower($property->address->state), \'property_state\')) {
if ($state != strtolower($property->address->state)) {
$state_id = wp_insert_term($property->address->state, \'property_state\', array(\'slug\' => strtolower($property->address->state)));
}
wp_set_post_terms($post_id, array($state_id[\'term_id\']), \'property_state\');
}else{
$term = get_term_by(\'slug\', strtolower($property->address->state), \'property_state\');
wp_set_post_terms($post_id, array($term->term_id), \'property_state\');
}
if($key == \'residential\' || $key == \'rental\'){
if(!term_exists( $property->authority->attributes()[\'value\'], \'property_status\')) {
if ($status != $property->authority->attributes()[\'value\']) {
$status_id = wp_insert_term(ucfirst($property->authority->attributes()[\'value\']), \'property_status\', array(\'slug\' => $property->authority->attributes()[\'value\']));
}
wp_set_post_terms($post_id, array($status_id[\'term_id\']), \'property_status\');
}else{
$term = get_term_by(\'slug\', $property->authority->attributes()[\'value\'], \'property_status\');
wp_set_post_terms($post_id, array($term->term_id), \'property_status\');
}
}else{
if(!term_exists( $property->commercialAuthority->attributes()[\'value\'], \'property_status\')) {
if ($status != $property->commercialAuthority->attributes()[\'value\']) {
$status_id = wp_insert_term(ucfirst($property->commercialAuthority->attributes()[\'value\']), \'property_status\', array(\'slug\' => $property->commercialAuthority->attributes()[\'value\']));
}
wp_set_post_terms($post_id, array($status_id[\'term_id\']), \'property_status\');
}else{
$term = get_term_by(\'slug\', $property->commercialAuthority->attributes()[\'value\'], \'property_status\');
wp_set_post_terms($post_id, array($term->term_id), \'property_status\');
}
}
$table_name = $wpdb->prefix . \'postmeta\';
$data1 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_bedrooms\',
\'meta_value\' => $property->features->bedrooms
);
$wpdb->insert($table_name, $data1, \'%s\');
$data2 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_bathrooms\',
\'meta_value\' => $property->features->bathrooms
);
$wpdb->insert($table_name, $data2, \'%s\');
$data3 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_garage\',
\'meta_value\' => $property->features->garages
);
$wpdb->insert($table_name, $data3, \'%s\');
$data4 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_id\',
\'meta_value\' => $property->uniqueID
);
$wpdb->insert($table_name, $data4, \'%s\');
$data5 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_price\',
\'meta_value\' => $property->price
);
$wpdb->insert($table_name, $data5, \'%s\');
$data6 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_price_postfix\',
\'meta_value\' => \'mo\'
);
$wpdb->insert($table_name, $data6, \'%s\');
$data7 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_map_address\',
\'meta_value\' => $property->address->streetNumber.\',\'.$property->address->street.\',\'.$property->address->suburb.\',\'.$property->address->state.\',\'.$property->address->postcode
);
$wpdb->insert($table_name, $data7, \'%s\');
$data8 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_location\',
\'meta_value\' => $property->Geocode->Longitude.\',\'.$property->Geocode->Latitude
);
$wpdb->insert($table_name, $data8, \'%s\');
$data9 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_country\',
\'meta_value\' => $property->address->state
);
$wpdb->insert($table_name, $data9, \'%s\');
$data10 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_address\',
\'meta_value\' => $property->address->street
);
$wpdb->insert($table_name, $data10, \'%s\');
$data11 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_zip\',
\'meta_value\' => $property->address->postcode
);
$wpdb->insert($table_name, $data11, \'%s\');
$media_id = media_sideload_image( $property->images->img[0]->attributes()[\'url\'], $post_id, \'\', $return = \'id\' );
$data12 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'_thumbnail_id\',
\'meta_value\' => $media_id
);
$wpdb->insert($table_name, $data12, \'%s\');
foreach ($property->images->img as $images){
if($images->attributes()[\'url\'] != \'\'){
$medias_id = media_sideload_image( $images->attributes()[\'url\'], $post_id, \'\', $return = \'id\' );
$data13 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_images\',
\'meta_value\' => $medias_id
);
$wpdb->insert($table_name, $data13, \'%s\');
}
}
$data14 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_zip\',
\'meta_value\' => $property->address->postcode
);
$wpdb->insert($table_name, $data14, \'%s\');
$data15 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'slide_template\',
\'meta_value\' => \'default\'
);
$wpdb->insert($table_name, $data15, \'%s\');
$data16 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_map_street_view\',
\'meta_value\' => \'show\'
);
$wpdb->insert($table_name, $data16, \'%s\');
$data17 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'houzez_geolocation_long\',
\'meta_value\' => $property->Geocode->Longitude
);
$wpdb->insert($table_name, $data17, \'%s\');
$data18 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'houzez_geolocation_lat\',
\'meta_value\' => $property->Geocode->Latitude
);
$wpdb->insert($table_name, $data18, \'%s\');
$data19 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_status\',
\'meta_value\' => $property->attributes()[\'status\']
);
$wpdb->insert($table_name, $data19, \'%s\');
$data20 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_authority\',
\'meta_value\' => $property->authority->attributes()[\'value\']
);
$wpdb->insert($table_name, $data20, \'%s\');
$data21 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_category\',
\'meta_value\' => $property->category->attributes()[\'name\']
);
$wpdb->insert($table_name, $data21, \'%s\');
$data22 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_unique_id\',
\'meta_value\' => $property->uniqueID
);
$wpdb->insert($table_name, $data22, \'%s\');
$data23 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_property_map\',
\'meta_value\' => \'1\'
);
$wpdb->insert($table_name, $data23, \'%s\');
$additional_array[] = array(
\'fave_additional_feature_title\' => \'Feature\',
\'fave_additional_feature_value\' => \'"\'.$property->features->otherFeatures.\'"\'
);
$data24 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'additional_features\',
\'meta_value\' => serialize($additional_array)
);
$wpdb->insert($table_name, $data24, \'%s\');
$data25 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_additional_features_enable\',
\'meta_value\' => \'enable\'
);
$wpdb->insert($table_name, $data25, \'%s\');
$data26 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_video_url\',
\'meta_value\' => $property->videoLink
);
$wpdb->insert($table_name, $data26, \'%s\');
$data27 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_video_url\',
\'meta_value\' => $property->videoLink
);
$wpdb->insert($table_name, $data27, \'%s\');
$data28 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'property_floorplan\',
\'meta_value\' => $property->objects->floorplan->attributes()[\'url\']
);
$wpdb->insert($table_name, $data28, \'%s\');
$data29 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_featured\',
\'meta_value\' => \'1\'
);
$wpdb->insert($table_name, $data29, \'%s\');
$data30 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_agents\',
\'meta_value\' => $agent_id
);
$wpdb->insert($table_name, $data30, \'%s\');
$data31 = array(
\'post_id\' => $post_id,
\'meta_key\' => \'fave_agent_display_option\',
\'meta_value\' => \'agent_info\'
);
$wpdb->insert($table_name, $data31, \'%s\');
// Agent meta
if($property->listingAgent->telephone->attributes()[\'type\'] == \'mobile\'){
$agentdata1 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'fave_agent_mobile\',
\'meta_value\' => $property->listingAgent->telephone
);
$wpdb->insert($table_name, $agentdata1, \'%s\');
}
if($property->listingAgent->telephone->attributes()[\'type\'] == \'BH\'){
$agentdata2 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'fave_agent_office_num\',
\'meta_value\' => $property->listingAgent->telephone
);
$wpdb->insert($table_name, $agentdata2, \'%s\');
}
$agentdata3 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'fave_agent_email\',
\'meta_value\' => $property->listingAgent->email
);
$wpdb->insert($table_name, $agentdata3, \'%s\');
$agentdata4 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'fave_agent_agencies\',
\'meta_value\' => $property->listingAgent->office_id
);
$wpdb->insert($table_name, $agentdata4, \'%s\');
$agentdata5 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'fave_agent_id\',
\'meta_value\' => $property->listingAgent->agentid
);
$wpdb->insert($table_name, $agentdata5, \'%s\');
$agentdata6 = array(
\'post_id\' => $agent_id,
\'meta_key\' => \'slide_template\',
\'meta_value\' => \'default\'
);
$wpdb->insert($table_name, $agentdata6, \'%s\');
/*update_post_meta($post_id,\'fave_property_bedrooms\',$property->features->bedrooms);
update_post_meta($post_id,\'fave_property_bathrooms\',$property->features->bathrooms);
update_post_meta($post_id,\'fave_property_garage\',$property->features->garages);
update_post_meta($post_id,\'fave_property_id\',$property->uniqueID);
update_post_meta($post_id,\'fave_property_price\',$property->price);
update_post_meta($post_id,\'fave_property_price_postfix\',\'mo\');
update_post_meta($post_id,\'fave_property_map_address\',$property->address->streetNumber.\',\'.$property->address->street.\',\'.$property->address->suburb.\',\'.$property->address->state.\',\'.$property->address->postcode);
update_post_meta($post_id,\'fave_property_location\',$property->Geocode->Longitude.\',\'.$property->Geocode->Latitude);
update_post_meta($post_id,\'fave_property_country\',$property->address->state);
update_post_meta($post_id,\'fave_property_address\',$property->address->street);
update_post_meta($post_id,\'fave_property_zip\',$property->address->postcode);
//update_post_meta($post_id,\'fave_video_url\',$property->address->postcode);
$media_id = media_sideload_image( $property->images->img[0]->attributes()[\'url\'], $post_id, \'\', $return = \'id\' );
update_post_meta($post_id,\'_thumbnail_id\',$media_id);
foreach ($property->images->img as $images){
$medias_id = media_sideload_image( $images->attributes()[\'url\'], $post_id, \'\', $return = \'id\' );
add_post_meta($post_id,\'fave_property_images\',$medias_id);
}
update_post_meta($post_id,\'fave_property_zip\',$property->address->postcode);
update_post_meta($post_id,\'slide_template\',\'default\');
update_post_meta($post_id,\'fave_property_map_street_view\',\'show\');
update_post_meta($post_id,\'houzez_geolocation_long\',$property->Geocode->Longitude);
update_post_meta($post_id,\'houzez_geolocation_lat\',$property->Geocode->Latitude);
update_post_meta($post_id,\'property_status\',$property->attributes()[\'status\']);
update_post_meta($post_id,\'property_authority\',$property->authority->attributes()[\'value\']);
update_post_meta($post_id,\'property_category\',$property->category->attributes()[\'name\']);
update_post_meta($post_id,\'property_unique_id\',$property->uniqueID);*/
if($key == \'residential\' || $key == \'rental\'){
$status = $property->authority->attributes()[\'value\'];
}else{
$status = $property->commercialAuthority->attributes()[\'value\'];
}
$state = strtolower($property->address->state);
}
// deleting the file
//unlink($target_file);
/* end storing in db */
//redirecting
//wp_redirect(admin_url(\'admin.php?page=custom-importer\', \'http\'), 301);
//end redirecting
} else {
echo "<br/><br/>Sorry, there was an error uploading your file.";
}
}
}