Skip to content

Instantly share code, notes, and snippets.

@hyperking
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyperking/be7010ced3b0f58b75f3 to your computer and use it in GitHub Desktop.
Save hyperking/be7010ced3b0f58b75f3 to your computer and use it in GitHub Desktop.
Wordpress JSON generator
function jsonGenerator(){
// Auto Generate Sitemaps after every new post
add_action("publish_post", "create_jsonfiles");
add_action("publish_page", "create_jsonfiles");
function create_jsonfiles() {
$postsForJson = get_posts(array(
'numberposts' => -1,
// 'orderby' => 'modified',
'post_type' => array('post','page'),
'order' => 'DESC' ));
foreach($postsForJson as $post) {
// setup_postdata( $post );
$filename = get_the_title($post->ID);
$postdate = explode(" ", $post->post_modified);
$data .= '{'."\n".
'"title":"'. get_the_title($post->ID) .'",'. "\n".
'"slug":"'. get_permalink($post->ID) .'",'. "\n".
'"content":"'. get_the_content($post->ID) .'",'. "\n".
'"pubdate":"'. $postdate[0] .'"'.
"\n"."}";
$fp = fopen(ABSPATH . $filename.".json", 'w');
fwrite($fp, $data); fclose($fp);
}
}
}
jsonGenerator();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment