Can you write me a plugin for WordPress that will Generate bulk pages based on a CSV that I upload? the keywords should be used as page title, URL, meta description
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a basic example of how you could structure a WordPress plugin to generate bulk pages based on a CSV file upload. This plugin will use the CSV file's first column for the page title and URL (slug), and the second column for the meta description. Please note, this is a simplified example to guide you through the process. You might need to adjust and expand it based on your specific requirements, such as error handling, security measures (e.g., validating and sanitizing inputs more rigorously), and compatibility with your WordPress version.
Plugin Name: Bulk Page Generator from CSV
File Structure:
bulk-page-generator-csv
bulk-page-generator-csv.php
readme.txt (Optional, for plugin description)
bulk-page-generator-csv.php:
php
<?php
/*
Plugin Name: Bulk Page Generator from CSV
Description: Generate bulk WordPress pages from a CSV file.
Version: 1.0
Author: Your Name
*/
// Hook for adding admin menu
add_action('admin_menu', 'bppgcsv_add_admin_menu');
function bppgcsv_add_admin_menu() {
// Check user capability before adding the menu
if (current_user_can('manage_options')) {
add_menu_page('Bulk Page Generator', 'Bulk Page Gen', 'manage_options', 'bulk-page-generator-csv', 'bppgcsv_settings_page');
}
}
// Settings page callback
function bppgcsv_settings_page() {
if (!current_user_can('manage_options')) {
// Display an error message if the user lacks permissions
echo '<div class="error">You do not have sufficient permissions to access this page.</div>';
return;
}
if (isset($_POST['bppgcsv_upload_csv'])) {
bppgcsv_handle_csv_upload();
}
?>
<div class="wrap">
<h1>Bulk Page Generator from CSV</h1>
<form method="post" enctype="multipart/form-data">
<?php
// Add nonce for security
wp_nonce_field('bppgcsv_nonce', 'bppgcsv_nonce_field');
?>
<input type="file" name="bppgcsv_file" accept=".csv" required>
<input type="submit" name="bppgcsv_upload_csv" value="Upload & Generate Pages">
</form>
<p>Please upload a CSV file with the first column as page titles (used for title and slug) and the second column as meta descriptions.</p>
</div>
<?php
}
function bppgcsv_handle_csv_upload() {
// Validate nonce for security
if (!wp_verify_nonce($_POST['bppgcsv_nonce_field'], 'bppgcsv_nonce')) {
die('Security check failed!');
}
// Retrieve the uploaded file
$uploaded_file = $_FILES['bppgcsv_file'];
// Check for upload errors
if ($uploaded_file['error'] !== UPLOAD_ERR_OK) {
echo '<div class="error">Upload failed. Error code: ' . $uploaded_file['error'] . '</div>';
return;
}
// Validate file type
if (pathinfo($uploaded_file['name'], PATHINFO_EXTENSION) !== 'csv') {
echo '<div class="error">Only CSV files are allowed.</div>';
return;
}
$file_path = $uploaded_file['tmp_name'];
if (($handle = fopen($file_path, 'r')) !== FALSE) {
$row = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Skip header row if present
if ($row++ === 0) continue;
// Validate row structure
if (count($data) < 2) {
echo '<div class="error">Invalid CSV format at row ' . ($row + 1) . '. Expected at least two columns.</div>';
continue;
}
// Sanitize inputs
$title = sanitize_text_field($data[0]);
$slug = sanitize_title($title); // Using title as slug
$meta_description = sanitize_text_field($data[1]);
// Check if page already exists
$existing_page = get_page_by_path($slug, OBJECT, 'page');
if ($existing_page) {
echo "<div class='update'>Page <strong>$title</strong> already exists. Skipped.</div>";
continue;
}
// Generate the page
$page_id = wp_insert_post(
array(
'post_title' => $title,
'post_name' => $slug,
'post_status' => 'publish',
'post_type' => 'page',
'meta_input' => array(
// Support for Yoast SEO and All in One SEO Pack
'_yoast_wpseo_metadesc' => $meta_description, // For Yoast SEO
'_aioseop_description' => $meta_description, // For All in One SEO Pack
),
)
);
if ($page_id && !is_wp_error($page_id)) {
echo "<div class='updated'>Page <strong>$title</strong> created successfully.</div>";
} else {
echo "<div class='error'>Failed to create page <strong>$title</strong>.</div>";
}
}
fclose($handle);
} else {
echo '<div class="error">Could not open file for reading.</div>';
}
}
Important Considerations:
Security and Validation: This example provides enhanced security checks, including nonce validation, user capability checks, and input sanitization.
Dependency on SEO Plugins for Meta Description: This script supports both Yoast SEO and All in One SEO Pack for setting the meta description. If you're using a different SEO plugin, you'll need to adjust the meta key accordingly.
CSV Format: The script expects a simple CSV with the title in the first column and the meta description in the second. Adjust the column indices in the $data array as needed.
Error Handling: The example provides improved error handling, including specific error messages for upload issues, file type validation, and row-level validation.
Testing: Thoroughly test this plugin in a staging environment before using it on a live site to ensure compatibility with your WordPress version and other installed plugins.
Installation:
Create a folder named bulk-page-generator-csv in your WordPress plugins directory (wp-content/plugins/).
Inside this folder, create a file named bulk-page-generator-csv.php.
Copy the provided PHP code into this file.
Navigate to your WordPress admin area, go to Plugins, and activate "Bulk Page Generator from CSV".
Find "Bulk Page Gen" in the admin menu, upload your CSV, and generate pages.