<?php
/**
* Plugin Name: WordPress CSV Post Uploader
* Description: Allow users to upload CSV files to create posts in the default WordPress posts table
* Plugin URI: https://wordpress.org
* Author: Wayne (Modified by Assistant)
* Author URI: https://wordpress.org
* Version: 1.0.1
*/
if (!defined('ABSPATH')) exit;
define('WCPU_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('WCPU_PLUGIN_URL', plugins_url());
class WP_CSV_Post_Uploader {
public function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('init', array($this, 'register_shortcodes'));
add_action('wp_ajax_process_post_csv', array($this, 'process_post_csv'));
add_action('wp_ajax_nopriv_process_post_csv', array($this, 'process_post_csv'));
}
public function enqueue_scripts() {
wp_enqueue_script('csv-posts-uploader-script', plugin_dir_url(__FILE__) . 'assets/uploader.js', array('jquery'), '1.0.1', true);
wp_localize_script('csv-posts-uploader-script', 'csv_upload_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('csv_upload_nonce')
));
}
public function register_shortcodes() {
add_shortcode('csv_post_upload_form', array($this, 'render_upload_form'));
}
public function render_upload_form() {
ob_start();
?>
<form id="csv-post-upload-form" enctype="multipart/form-data">
<input type="file" name="post_csv_file" accept=".csv" required>
<button type="submit">Upload CSV</button>
</form>
<div id="upload-result"></div>
<?php
return ob_get_clean();
}
public function process_post_csv() {
check_ajax_referer('csv_upload_nonce', 'nonce');
if (!current_user_can('edit_posts')) {
wp_send_json_error('You don\'t have the permission to create posts.');
}
if (empty($_FILES['post_csv_file'])) {
wp_send_json_error('No file was uploaded');
}
$file = $_FILES['post_csv_file'];
$uploaded_file = wp_handle_upload($file, array('test_form' => false));
if (isset($uploaded_file['error'])) {
wp_send_json_error('Error uploading file: ' . $uploaded_file['error']);
}
$result = $this->insert_posts_from_csv($uploaded_file['file']);
unlink($uploaded_file['file']);
if ($result['success']) {
wp_send_json_success($result['message']);
} else {
wp_send_json_error($result['message']);
}
}
private function insert_posts_from_csv($file_path) {
if (($handle = fopen($file_path, "r")) !== FALSE) {
$header = fgetcsv($handle, 1000, ",");
$required_columns = array('post_title', 'post_content');
if (count(array_intersect($required_columns, $header)) !== count($required_columns)) {
return array('success' => false, 'message' => 'CSV file must contain post_title and post_content columns.');
}
$inserted_count = 0;
$skipped_count = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (count($header) === count($data)) {
$post_data = array_combine($header, $data);
// Ensure required fields are not empty
if (empty($post_data['post_title']) || empty($post_data['post_content'])) {
$skipped_count++;
continue;
}
// Add necessary default values
$post_data = array_merge([
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_date' => current_time('mysql'),
'post_date_gmt' => current_time('mysql', 1),
'comment_status' => get_option('default_comment_status'),
'ping_status' => get_option('default_ping_status'),
'post_password' => '',
'post_name' => sanitize_title($post_data['post_title']),
'to_ping' => '',
'pinged' => '',
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql', 1),
'post_parent' => 0,
'menu_order' => 0,
'post_type' => 'post',
'post_mime_type' => '',
'comment_count' => 0
], $post_data);
$post_id = wp_insert_post($post_data, true);
if (!is_wp_error($post_id)) {
$inserted_count++;
} else {
$skipped_count++;
error_log('Error inserting post: ' . $post_id->get_error_message());
}
}
}
fclose($handle);
return array(
'success' => true,
'message' => "Processed CSV file: Inserted $inserted_count posts, Skipped $skipped_count rows."
);
}
return array('success' => false, 'message' => 'Error processing CSV file.');
}
}
$wp_csv_post_uploader = new WP_CSV_Post_Uploader();