$('.import-btn').on('click', function() {
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: "POST",
data: {
action: 'import_data'
},
success: function(data) {
if (data !== 0) {
Swal.fire({
icon: "success",
title: "Imported data",
text: "Thankyou for importing this file",
});
} else {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!",
});
}
},
error: function() {
alert('Error');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<?php
add_action('wp_ajax_nopriv_import_data', 'import_data');
add_action('wp_ajax_import_data', 'import_data');
function import_data()
{
function upload_image_from_url($thumbnail_url)
{
$image_name = basename($thumbnail_url);
$image_data = file_get_contents($thumbnail_url);
$upload_dir = wp_upload_dir();
$image_path = $upload_dir['path'] . '/' . $image_name;
if ($image_data === false) {
return new WP_Error('image_fetch_failed', 'Failed to fetch image data');
}
$image_saved = file_put_contents($image_path, $image_data);
if ($image_saved == FALSE) {
return new WP_Error('Upload failed', 'Failed to upload file');
}
return $image_path;
}
function insert_image_to_wp_posts($image_path, $post_id)
{
$upload_dir = wp_upload_dir();
$image_url = str_replace($upload_dir['path'], $upload_dir['url'], $image_path);
$file_info = array(
'guid' => $image_url,
'post_mime_type' => mime_content_type($image_path),
'post_title' => sanitize_file_name(pathinfo($image_path, PATHINFO_FILENAME)),
'post_status' => 'inherit',
'post_parent' => $post_id,
);
$attachment_id = wp_insert_attachment($file_info, $image_path, $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_metadata = wp_generate_attachment_metadata($attachment_id, $image_path);
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
return $attachment_id;
}
$csvFile = get_template_directory() . '/movie_data.csv';
if (!file_exists($csvFile)) {
echo 'CSV file not found';
return;
}
if (($handle = fopen($csvFile, 'r')) !== FALSE) {
$header = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$post_title = isset($data[1]) ? $data[1] : '';
$post_date = isset($data[2]) ? $data[2] : '';
$category = isset($data[3]) ? $data[3] : '';
$thumbnail_url = isset($data[4]) ? $data[4] : '';
$post_content = "Movies";
// Creating post
$post_data = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'movie',
'post_date' => $post_date,
);
if ($post_data):
$post_id = wp_insert_post($post_data);
update_post_meta($post_id, 'category', $category);
if ($thumbnail_url) {
$image_path = upload_image_from_url($thumbnail_url);
if (!is_wp_error($image_path)) {
$attachment_id = insert_image_to_wp_posts($image_path, $post_id);
if (!is_wp_error($attachment_id)) {
update_post_meta($post_id, "_thumbnail_id", $attachment_id);
}
} else {
echo "Error while inserting image into WP Posts";
}
} else {
error_log('thumbnail url not generated');
}
endif;
}
fclose($handle);
echo '1'; // Success
} else {
echo '0'; // Failure opening the file
}
}?>