How to Import CSV Data into Custom Content Types (CCTs) Using WordPress and JetEngine

RAFAEL SIECZKOWSKI
Free
How to Import CSV Data into Custom Content Types (CCTs) Using WordPress and JetEngine
Advanced Customizations
Descripción :

When managing a business, importing data from CSV files is a powerful way to save time and avoid manual input errors. In this tutorial, we’ll explore how to import CSV files into Custom Content Types (CCTs) using WordPress and the JetEngine plugin.

This guide assumes basic knowledge of WordPress, PHP, and database management. We’ll break down the process of creating a function to import customer data (like names and email addresses) from a CSV file into the "Customers" CCT.

Understanding CCTs and JetEngine

CCTs (Custom Content Types) in JetEngine provide a more flexible and efficient way to store data compared to Custom Post Types (CPTs). Unlike CPTs, CCTs don’t use the WordPress post table but instead rely on custom database tables. This makes them ideal for handling large datasets or complex relationships.

Goal of the Tutorial

We will create a WordPress shortcode that allows users to upload a CSV file via the front end. The script will read this file, validate the data, and insert it into the CCT’s database table. The focus will be on importing customers' names and emails into the cliente_cct CCT, with the database table wp_jet_cct_cliente_cct.

Solution :

Step-by-Step Guide to Importing CSV Data into CCTs

Let’s dive into the steps and code required for this functionality.

Step 1: Set Up the CCT

Before importing, ensure you’ve created the necessary CCT in JetEngine. In this case, the CCT is called "cliente_cct" (customers) with fields for:

  • nome_do_cliente (Customer Name)
  • e_mail_do_cliente (Customer Email)

The CCT is stored in the table wp_jet_cct_cliente_cct.

Step 2: Write the CSV Import Function

Here’s the PHP code to handle the CSV import. It reads the uploaded file, processes each line, and inserts the data into the appropriate fields of the CCT.

<?php

function importar_clientes_cct_shortcode() {
if (isset($_POST['submit']) && !empty($_FILES['csv_file']['tmp_name'])) {
global $wpdb;
$csv_file = $_FILES['csv_file']['tmp_name'];
$handle = fopen($csv_file, 'r');

// Define the correct delimiter (here it's set to a semicolon by default)
$delimiter = ';';

// Read and skip the header row
$header = fgetcsv($handle, 1000, $delimiter);

// Define the table name for the CCT
$table_name = 'wp_jet_cct_cliente_cct';

while (($col = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
// Ensure at least one column for the customer name
if (isset($col[0])) {
$nome_cliente = sanitize_text_field($col[0]); // Customer Name
$email_cliente = isset($col[1]) ? sanitize_email($col[1]) : ''; // Email is optional
} else {
continue; // Skip the row if the customer name is missing
}

// Check if the customer name is provided
if (empty($nome_cliente)) {
echo "Name missing for row: " . implode(', ', $col) . "
";
continue; // Skip to the next row in the CSV
}

// Data to be inserted
$dados_para_inserir = array(
'nome_do_cliente' => $nome_cliente,
'e_mail_do_cliente' => $email_cliente,
);

// Data formats for each column
$formatos = array(
'%s', // nome_do_cliente
'%s', // e_mail_do_cliente
);

// Insert data into the CCT table
$wpdb->insert(
$table_name,
$dados_para_inserir,
$formatos
);

// Success or error message
if ($wpdb->insert_id) {
echo "Client imported successfully: " . $nome_cliente . "
";
} else {
echo "Error importing client: " . $wpdb->last_error . "
";
}
}

fclose($handle);
unlink($csv_file); // Delete the CSV file after import
}

ob_start();
?>

Import Customers


<?php
return ob_get_clean();
}
add_shortcode('importar_clientes_cct', 'importar_clientes_cct_shortcode');

Step 3: How the Code Works

  1. File Upload and Delimiter:
    • The script reads the CSV file, which is uploaded via a form, using fgetcsv(). The default delimiter is a semicolon (;), but you can modify it to fit your file format.
  2. Data Sanitization:
    • It’s crucial to sanitize inputs before processing. We use sanitize_text_field() for the customer name and sanitize_email() for the email.
  3. Database Insertion:
    • The $wpdb->insert() function inserts the sanitized data into the custom CCT table (wp_jet_cct_cliente_cct). The table structure is handled by JetEngine when the CCT is created.
  4. Handling Errors:
    • If a row lacks a name, it skips that row and moves on. After each row is processed, a success or error message is displayed based on the result of the database insert operation.

Step 4: Using the Shortcode

To allow your clients to import their data, add the shortcode [importar_clientes_cct] to any page or post. This will display a file upload form on the front end.

Final Thoughts

With this simple CSV import functionality, you can automate the process of importing customer data into your WordPress site. Whether you manage hundreds of customers or just a few, using CCTs with JetEngine and WordPress makes managing and displaying this data easy.

If you have any questions or need further customization, feel free to leave a comment below. Let us know how you use this technique in your projects!

Did you want to see this content ?
Scroll to Top