Last active
August 29, 2015 14:01
-
-
Save legenderrys/be7010ced3b0f58b75f3 to your computer and use it in GitHub Desktop.
Wordpress JSON generator
This file contains hidden or 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
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