<?php
/** Setup WordPress */
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
/** Set file path */
$file = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/liquid/import.json';
/** Remove existing files */
if (file_exists($file)) {
unlink($file);
}
/** Move the uploaded file to the $file path */
move_uploaded_file($_FILES['import']['tmp_name'], $file);
/**
* Take imported data and insert it into the database
* Delete the import file after it's been imported
*/
if (file_exists($file)) {
$contents = file_get_contents($file);
$json = json_decode($contents);
foreach($json->object_name as $item) {
$user_id = get_current_user_id();
/**
* Setup Post Data here to be inserted into the database
* include post type and meta values (Advanced Custom Fields)
*/
$args= [
'post_author' => $user_id,
'post_content' => '',
'post_title' => $item->json_key,
'post_status' => 'publish',
'post_type' => 'your_post_type',
'meta_input' => [
'custom_field_name' => $item->json_key
]
];
wp_insert_post($args);
}
unlink($file);
echo "JSON Data was imported! :)";
} else {
echo "Something went wrong...";
}