Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* First Run Create the Pages */
- if (is_admin()){
- if (!get_option('demo-data'))cms_create_default();
- }
- /* This function Reads the file */
- function cms_create_default(){
- /* Insert the categories if exists */
- /* category name option parent category name */
- $fileName = TEMPLATEPATH.'/demo-data/categories.txt';
- if (file_exists($fileName)){
- //$readfile[0] would be the first line and the headers the array begins at 0
- $readFile = file($fileName);
- if ($readFile){
- for ($k=0; $k<=count($readFile)-1; $k++) {
- if ($k != 0){
- $fields = split("\t",$readFile[$k]);
- cms_insert_category($fields);
- }
- }
- }
- }
- /* insert the posts if file exists */
- /* title category status excert content tags */
- $fileName = TEMPLATEPATH.'/demo-data/posts.txt';
- if (file_exists($fileName)){
- //$readfile[0] would be the first line and the headers the array begins at 0
- $readFile = file($fileName);
- if ($readFile){
- for ($k=0; $k<=count($readFile)-1; $k++) {
- if ($k != 0){
- $fields = split("\t",$readFile[$k]);
- cms_insert_post($fields);
- }
- }
- }
- }
- update_option('demo-data',true);
- }
- /* Create the Categories for our Posts */
- function cms_insert_category($catFields){
- $taxonomy = 'category';
- $catName = trim($catFields[0]);
- $catDescription = trim($catFields[1]);
- /* if we have a category name then insert the category */
- if(!is_term($catName, $taxonomy) && $catName){
- $niceName = str_replace(" ","-",$catName);
- $niceName = strtolower($niceName);
- $cat_defaults=array();
- $insertid = array();
- $cat_defaults = array(
- 'name' => $catName,
- 'description' => $catDescription);
- $temp = wp_insert_term($catName, $taxonomy, $cat_defaults);
- }
- }
- /* Insert the post */
- function cms_insert_post($postFields){
- $posttype = trim($postFields[0]);
- $title = trim($postFields[1]);
- $category = trim($postFields[2]);
- $status = trim($postFields[3]);
- $excert = trim($postFields[4]);
- $content = trim($postFields[5]);
- $tags = trim($postFields[6]);
- $posttype = trim($postFields[7]);
- $catID=array();
- if ($category) $catID[] = cms_get_categoryID($category);
- if ($status){
- $status='publish';
- }else{
- $status='draft';
- }
- if (!$posttype=='page') $posttype = 'post';
- $cms_post = array();
- $cms_post['post_type'] = $posttype;
- $cms_post['post_title'] = $title;
- $cms_post['post_content'] = $content;
- $cms_post['post_excerpt'] = $excerpt;
- $cms_post['post_status'] = $status;
- $cms_post['post_author'] = 1;
- $cms_post['post_category'] = $catID;
- $post_ID = wp_insert_post($cms_post);
- /* insert any tags associated with the post */
- if ($tags && $post_ID) wp_set_post_terms( $post_ID, $tags);
- }
- function cms_get_categoryID($catName){
- global $wpdb;
- $category_slug = str_replace(" ","-",$catName);
- $category_slug = strtolower($category_slug);
- $thevalue = $wpdb->get_var( "SELECT term_id FROM $wpdb->terms WHERE slug = '" . $category_slug . "'" );
- if ($thevalue) {
- return $thevalue;
- }else{
- return "";
- }
- }
- ?>
Add Comment
Please, Sign In to add comment