Created
May 30, 2016 23:59
-
-
Save PabloWestphalen/91f004ce4c013a0edabc964c34ad559c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL|E_STRICT); | |
//include the files we need to work with WordPress in an independent script | |
require_once("../wp-config.php"); | |
require_once('../wp-admin/includes/taxonomy.php'); | |
$wp->init(); $wp->parse_request(); $wp->query_posts(); | |
$wp->register_globals(); $wp->send_headers(); | |
global $wpdb; | |
global $current_user; | |
$current_user = wp_get_current_user(); | |
//get the data to be imported | |
$string = file_get_contents("gallery.json"); | |
$images = json_decode($string, true); | |
//import loop | |
foreach($images as $image) { | |
// Create post object | |
$image_post = array( | |
'post_title' => $image["Title"], | |
'post_title' => $image["Category"] . " " . $image["Image number"], | |
'post_status' => 'publish', | |
'post_type' => 'post', | |
'post_content' => ' ', | |
); | |
// Insert the post into the database | |
$image_id = wp_insert_post( $image_post, true ); | |
//echo "created this image id: " . $image_id . "\n"; | |
//Get the category id if it already exists, or create one otherwise | |
$category_id = get_cat_ID($image["Category"]); | |
if($category_id == 0) { | |
$category_id = wp_create_category($image["Category"]); | |
} | |
wp_set_post_categories($image_id, array($category_id)); | |
//Set some custom fields (created by the ACF plugin) | |
update_field('field_55f3b64c173d5', $image["Medium"], $image_id); | |
update_field('field_55f3b66b173d6', $image["Size"], $image_id); | |
update_field('field_55f3b673173d7', $image["Creation Year"], $image_id); | |
uploadAttachmentImage($image, $image_id); | |
} | |
function uploadAttachmentImage($image, $image_id) { | |
$filename = $image["Image number"] . ".jpg"; | |
$filename = "import/" . $image["Category"] . "/" . $filename; | |
if(file_exists($filename) && !is_dir($filename)) { | |
// Check the type of file. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( basename( $filename ), null ); | |
// Get the path to the upload directory. | |
$wp_upload_dir = wp_upload_dir(); | |
// Prepare an array of post data for the attachment. | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => $image["Title"], | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $filename, $image_id ); | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( '../wp-admin/includes/image.php' ); | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
set_post_thumbnail( $image_id, $attach_id ); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{ | |
"Image number": 1, | |
"Title": "A cool Picture", | |
"Medium": "Oil", | |
"Size": "10 inch", | |
"Creation Year": "2016", | |
"Category": "Interior" | |
}, { | |
"Image number": 2, | |
"Title": "Sea drawing", | |
"Medium": "Oil on canvas", | |
"Size": "10 inch", | |
"Creation Year": "2016", | |
"Category": "Interior" | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment