Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Page template ton insert via json file
<?php
//Template Name: Insert Json File
//Testing Mode First
$test = true;
//If we've uploaded our JSON file with our theme
$url = get_template_directory_uri() . 'json/my-new-posts.json';
//Convert our JSON into a PHP Array
$array = json_decode(file_get_contents($url),true);
if($array) {
if($test){ //Testing mode simply outputs the data onto the screen to verify
foreach ($array as $row){ //Loop through the feed and output each row
echo 'Post Title:' . $row[title] . '<br>';
echo 'Post Content:' . $row[content] . '<br><br>';
}
}else{ //Live mode actually inserts the post into our database
foreach ($array as $row) { //Loop through the feed so we can insert each post
$post_arr = array(
'post_title' => $row[title], //Title of post
'post_type' =>'post', //could be any custom post type
'post_content' => $row[content] //Post Content
);
wp_insert_post($post_arr, true);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.