Created
December 25, 2018 06:37
-
-
Save ganmahmud/4e28841435a0392bb12fef1d00450e9a to your computer and use it in GitHub Desktop.
Excel file (.csv) to WP post Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('admin_menu', 'excel2post_plugin_setup_menu'); | |
function excel2post_plugin_setup_menu(){ | |
add_menu_page('Shoppers Mag CSV Import Page', 'Shoppers Mag CSV Import', 'manage_options', 'csv-import', 'e2p', "dashicons-image-rotate-right", 2); | |
} | |
function e2p(){ | |
e2p_handle_post(); | |
?> | |
<h1>CSV Import</h1> | |
<h2>Upload a File</h2> | |
<!-- Form to handle the upload - The enctype value here is very important --> | |
<form method="post" enctype="multipart/form-data"> | |
<input type='file' id='test_upload_pdf' name='test_upload_pdf'></input> | |
<?php | |
submit_button('Upload') ?> | |
</form> | |
<?php | |
?> | |
<?php | |
} | |
function e2p_handle_post(){ | |
// First check if the file appears on the _FILES array | |
if (isset($_FILES['test_upload_pdf'])) { | |
$pdf = $_FILES['test_upload_pdf']; | |
// Use the wordpress function to upload | |
// test_upload_pdf corresponds to the position in the $_FILES array | |
// 0 means the content is not associated with any other posts | |
$uploaded = media_handle_upload('test_upload_pdf', 0); | |
// Error checking using WP functions | |
if (is_wp_error($uploaded)) { | |
echo "Error uploading file: " . $uploaded->get_error_message(); | |
} | |
else { | |
echo "<h3 style='color:green;'>File upload successful!</h3>"; | |
$dir = wp_upload_dir(); | |
global $csv_url; | |
$csv_url = $dir["path"] . "/" . $pdf["name"]; | |
$csv_http_url = $dir["url"] . "/" . $pdf["name"]; | |
print_r($csv_url); | |
echo "<p>"; | |
echo "To insert the posts into the database, click the button to the right."; | |
echo '<form action="' . get_site_url() . '/wp-admin/admin-post.php" method="post">'; | |
echo '<input type="hidden" name="action" value="create_post">'; | |
echo '<input type="submit" value="Submit" class="button button-primary">'; | |
echo '</form>'; | |
echo "</p>"; | |
$_SESSION["csv_url"] = $csv_url; | |
$_SESSION["csv_http_url"] = $csv_http_url; | |
} | |
} | |
} | |
add_action('admin_post_create_post', 'create_post_from_csv'); | |
function pn_get_attachment_id_from_url($attachment_url = ''){ | |
global $wpdb; | |
$attachment_id = false; | |
// If there is no url, return. | |
if ('' == $attachment_url) return; | |
// Get the upload directory paths | |
$upload_dir_paths = wp_upload_dir(); | |
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image | |
if (false !== strpos($attachment_url, $upload_dir_paths['baseurl'])) { | |
// If this is the URL of an auto-generated thumbnail, get the URL of the original image | |
$attachment_url = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url); | |
// Remove the upload path base directory from the attachment URL | |
$attachment_url = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachment_url); | |
// Finally, run a custom database query to get the attachment ID from the modified attachment URL | |
$attachment_id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url)); | |
} | |
return $attachment_id; | |
} | |
function create_post_from_csv(){ | |
global $wpdb; | |
$post_data = array( | |
'post_type' => 'post' | |
); | |
$file = $_SESSION["csv_url"]; | |
$data = array(); | |
$errors = array(); | |
if (!is_readable($file)) { | |
chmod($file, 0744); | |
} | |
if (is_readable($file) && $_file = fopen($file, "r")) { | |
$post = array(); | |
$header = fgetcsv($_file); | |
while ($row = fgetcsv($_file)) { | |
foreach($header as $i => $key) { | |
$post[$key] = $row[$i]; | |
} | |
$data[] = $post; | |
} | |
fclose($_file); | |
} | |
else { | |
$errors[] = "File '$file' could not be opened. Check the file's permissions to make sure it's readable by your server."; | |
} | |
if (!empty($errors)) { | |
print_r($errors); | |
} | |
// echo "<pre>"; | |
// print_r($data); | |
// echo "</pre>"; | |
// =================================== | |
$offers = $wpdb->get_col("SELECT post_title FROM {$wpdb->posts} WHERE post_type = 'offer'"); | |
$businesses = $wpdb->get_col("SELECT post_title FROM {$wpdb->posts} WHERE post_type = 'business' AND post_status = 'publish'"); | |
foreach($data as $value) { | |
if (in_array($value['Business Name'], $businesses)) { | |
$offer_title = mysql_real_escape_string($value['Offer Title']); | |
$offer_description = mysql_real_escape_string($value['Offer Description']); | |
$offer_legal = mysql_real_escape_string($value['Legal Terms']); | |
$offer_start = mysql_real_escape_string($value['Start Date']); | |
$offer_end = mysql_real_escape_string($value['End Date']); | |
$offer_small_sub = $offer_legal . "<br />Fecha de inicio - " . $offer_start . "<br />Fecha final - " . $offer_end; | |
/*==========================Now create offer here======================*/ | |
$new_offers = array( | |
'post_title' => $offer_title, | |
'post_content' => $offer_description, | |
'post_type' => 'offer', | |
'post_status' => 'publish', | |
); | |
$cat = $value['category']; | |
$offer_post_id = wp_insert_post($new_offers, true); // created offer | |
echo "<p style='color:green;'>" . $offer_title . " - Offer Imported</p>"; | |
wp_set_object_terms($offer_post_id, $cat, 'offer_category'); | |
$thumb_id = pn_get_attachment_id_from_url(trim($value['Offer Feature Image Link'])); | |
set_post_thumbnail($offer_post_id, $thumb_id); | |
$offer_metas = array( | |
"bus_name" => $value["Business Name"], | |
"business_name" => $value["Business Name"], | |
"offer_location" => $value["Business Location"], | |
"offer_sub_description" => $offer_small_sub, | |
); | |
update_post_meta($offer_post_id, 'SkilledTheme_Themag_SKL_offer_meta', $offer_metas); | |
// offer ends | |
continue; | |
} | |
$new_business = array( | |
'post_title' => $value['Business Name'], | |
'post_content' => $value['Business Description'], | |
'post_type' => 'business', | |
'post_status' => 'publish', | |
); | |
$cat = $value['category']; | |
// $c_id = get_cat_ID($cat); | |
$the_post_id = wp_insert_post($new_business); | |
wp_set_object_terms($the_post_id, $cat, 'business_category'); | |
$thumb_id = pn_get_attachment_id_from_url(trim($value['Logo Link'])); | |
set_post_thumbnail($the_post_id, $thumb_id); | |
$metas = array( | |
"business_location" => $value["Business Location"], | |
"latitude" => $value["Latitude"], | |
"longitude" => $value["Longitude"], | |
"business_contact" => $value["Telephone"], | |
); | |
update_post_meta($the_post_id, 'SkilledTheme_Themag_SKL_business_meta', $metas); | |
/*==========================Now create offer here======================*/ | |
$offer_title = mysql_real_escape_string($value['Offer Title']); | |
$offer_description = mysql_real_escape_string($value['Offer Description']); | |
$offer_legal = mysql_real_escape_string($value['Legal Terms']); | |
$offer_start = mysql_real_escape_string($value['Start Date']); | |
$offer_end = mysql_real_escape_string($value['End Date']); | |
$offer_small_sub = $offer_legal . "<br />Fecha de inicio - " . $offer_start . "<br />Fecha final - " . $offer_end; | |
/*==========================Now create offer here======================*/ | |
$new_offers = array( | |
'post_title' => $offer_title, | |
'post_content' => $offer_description, | |
'post_type' => 'offer', | |
'post_status' => 'publish', | |
); | |
$offer_post_id = wp_insert_post($new_offers, true); // created offer | |
echo "<p style='color:green;'>" . $offer_title . " - Offer Imported</p>"; | |
wp_set_object_terms($offer_post_id, $cat, 'offer_category'); | |
$thumb_id = pn_get_attachment_id_from_url(trim($value['Offer Feature Image Link'])); | |
set_post_thumbnail($offer_post_id, $thumb_id); | |
$offer_metas = array( | |
"bus_name" => $value["Business Name"], | |
"business_name" => $value["Business Name"], | |
"offer_location" => $value["Business Location"], | |
"offer_sub_description" => $offer_small_sub, | |
); | |
update_post_meta($offer_post_id, 'SkilledTheme_Themag_SKL_offer_meta', $offer_metas); | |
// offer ends | |
} | |
echo '<a href="' . get_site_url() . '/wp-admin/edit.php?post_type=business" target="_blank">Check Business Entry</a><br />'; | |
echo '<a href="' . get_site_url() . '/wp-admin/edit.php?post_type=offer" target="_blank">Check Offer Entry</a>'; | |
$file_id = pn_get_attachment_id_from_url($_SESSION["csv_http_url"]); | |
wp_delete_attachment($file_id, true); //to delete uploaded csv file from the media library | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment