Digitalraindrops

WordPress Create Categories and Posts

Oct 12th, 2010
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2.  
  3. /* First Run Create the Pages */
  4. if (is_admin()){
  5.     if (!get_option('demo-data'))cms_create_default();
  6. }
  7.  
  8. /* This function Reads the file */
  9. function cms_create_default(){
  10.     /* Insert the categories if exists */
  11.     /* category name option parent category name */
  12.     $fileName = TEMPLATEPATH.'/demo-data/categories.txt';
  13.     if (file_exists($fileName)){
  14.         //$readfile[0] would be the first line and the headers the array begins at 0
  15.         $readFile = file($fileName);
  16.         if ($readFile){
  17.             for ($k=0; $k<=count($readFile)-1; $k++) {
  18.                 if ($k != 0){
  19.                     $fields = split("\t",$readFile[$k]);
  20.                     cms_insert_category($fields);
  21.                 }
  22.             }
  23.         }
  24.     }
  25.     /* insert the posts if file exists */
  26.     /* title category status excert content tags */
  27.     $fileName = TEMPLATEPATH.'/demo-data/posts.txt';
  28.     if (file_exists($fileName)){
  29.         //$readfile[0] would be the first line and the headers the array begins at 0
  30.         $readFile = file($fileName);
  31.         if ($readFile){
  32.             for ($k=0; $k<=count($readFile)-1; $k++) {
  33.                 if ($k != 0){
  34.                     $fields = split("\t",$readFile[$k]);
  35.                     cms_insert_post($fields);
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     update_option('demo-data',true);
  41. }
  42.  
  43. /* Create the Categories for our Posts */
  44. function cms_insert_category($catFields){
  45.     $taxonomy = 'category';
  46.     $catName = trim($catFields[0]);
  47.     $catDescription = trim($catFields[1]);
  48.     /* if we have a category name then insert the category */
  49.     if(!is_term($catName, $taxonomy) && $catName){
  50.         $niceName = str_replace(" ","-",$catName);
  51.         $niceName = strtolower($niceName);
  52.         $cat_defaults=array();
  53.         $insertid = array();
  54.         $cat_defaults = array(
  55.             'name' => $catName,
  56.             'description' => $catDescription);
  57.         $temp = wp_insert_term($catName, $taxonomy, $cat_defaults);
  58.     }
  59. }
  60.  
  61. /* Insert the post */
  62. function cms_insert_post($postFields){
  63.     $posttype = trim($postFields[0]);
  64.     $title = trim($postFields[1]);
  65.     $category = trim($postFields[2]);
  66.     $status = trim($postFields[3]);
  67.     $excert = trim($postFields[4]);
  68.     $content = trim($postFields[5]);
  69.     $tags = trim($postFields[6]);
  70.     $posttype = trim($postFields[7]);
  71.     $catID=array();
  72.     if ($category) $catID[] = cms_get_categoryID($category);
  73.     if ($status){
  74.         $status='publish';
  75.     }else{
  76.         $status='draft';
  77.     }
  78.     if (!$posttype=='page') $posttype = 'post';
  79.     $cms_post = array();
  80.     $cms_post['post_type'] = $posttype;
  81.     $cms_post['post_title'] = $title;
  82.     $cms_post['post_content'] = $content;
  83.     $cms_post['post_excerpt'] = $excerpt;
  84.     $cms_post['post_status'] = $status;
  85.     $cms_post['post_author'] = 1;
  86.     $cms_post['post_category'] = $catID;
  87.     $post_ID = wp_insert_post($cms_post);
  88.     /* insert any tags associated with the post */
  89.     if ($tags && $post_ID) wp_set_post_terms( $post_ID, $tags);
  90. }
  91.  
  92. function cms_get_categoryID($catName){
  93.     global $wpdb;
  94.     $category_slug = str_replace(" ","-",$catName);
  95.     $category_slug = strtolower($category_slug);
  96.     $thevalue = $wpdb->get_var( "SELECT term_id FROM $wpdb->terms WHERE slug = '" . $category_slug . "'" );
  97.     if ($thevalue) {
  98.         return $thevalue;
  99.     }else{ 
  100.         return "";
  101.     }
  102. }
  103.  
  104. ?>
Add Comment
Please, Sign In to add comment