Skip to content

Instantly share code, notes, and snippets.

@bacoords
Last active December 8, 2016 22:27
Show Gist options
  • Save bacoords/f9d7d3911f7f2197e0d242739e6bcfd6 to your computer and use it in GitHub Desktop.
Save bacoords/f9d7d3911f7f2197e0d242739e6bcfd6 to your computer and use it in GitHub Desktop.
Page template to insert Google Sheets JSON feed into WordPress
<?php
//Template Name: Insert Json from Google Sheets
//Testing Mode First
$test = true;
//Google Sheet JSON Feed Url
$url = 'https://spreadsheets.google.com/feeds/list/{{UNIQUE-ID}}/od6/public/values?alt=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[feed][entry] as $row){ //Loop through the feed and output each row
echo 'Post Title:' . $row['gsx$title']['$t'] . '<br>';
echo 'Post Content:' . $row['gsx$content']['$t'] . '<br><br>';
}
}else{ //Live mode actually inserts the post into our database
foreach ($array[feed][entry] as $row) { //Loop through the feed so we can insert each post
$post_arr = array(
'post_title' => $row['gsx$title']['$t'], //Title of post
'post_type' =>'post', //could be any custom post type
'post_content' => $row['gsx$content']['$t']
);
wp_insert_post($post_arr, true);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment