No titles given'; } else { //Is magic quotes on? if (get_magic_quotes_gpc()) { // Yes? Strip the added slashes $_POST = array_map('stripslashes', $_POST); } //logic switch ($_POST['postorpage']) { case 'post' : $page_or_post = 'post'; break; case 'page' : $page_or_post = 'page'; break; } $author_id = 1; // Default admin user_id // Check user input $user_query = "SELECT ID, user_login, display_name, user_email FROM $wpdb->users WHERE ID = ".intval($_POST['author_id']) . " LIMIT 1"; $users = $wpdb->get_results($user_query); foreach ($users AS $row) { // User found, replace value of $author $author_id = $row->ID; } // Foreach line $titles = explode("\n", $_POST['titles']); // Page? if($page_or_post == 'page') { $i = 0; $post_parent_org = intval($_POST['post_parent']); $newarray = array(); $lastlevel = $post_parent_org; foreach ($titles as $title) { $title = trim($title); $level = substr_count($title, '-'); $title = str_replace('-','',$title); // Remove spaces before and after titles $title = trim($title); // Init $newarray[$i] = array('level' => $level,'lastlevel' => $lastlevel,'title' => $title, 'child_of_page_id' => $post_parent_org, 'page_id' => NULL); // First child? if($level > $lastlevel) { $newarray[$i]['child_of_page_id'] = $newarray[$i-1]['page_id']; $newarray[$i]['child_of_page_title'] = $newarray[$i-1]['title']; } // Same sub as previous? if($level == $lastlevel) { // Go back to find sub $j = $i;$continue = true; while($j >= 0 && $continue) { if($level > $newarray[$j]['level']) { $newarray[$i]['child_of_page_id'] = $newarray[$j]['page_id']; $newarray[$i]['child_of_page_title'] = $newarray[$j]['title']; $continue = false; } $j--; } } // Second child, but after a child-child? if($level < $lastlevel) { // Go back to find sub $j = $i;$continue = true; while($j >= 0 && $continue) { if($level > $newarray[$j]['level']) { $newarray[$i]['child_of_page_id'] = $newarray[$j]['page_id']; $newarray[$i]['child_of_page_title'] = $newarray[$j]['title']; $continue = false; } $j--; } } // Now insert // Create post object $post = array (); $post['post_title'] = $title; $post['post_type'] = $page_or_post; $post['post_content'] = ''; $post['post_status'] = 'publish'; $post['post_author'] = $author_id; if($page_or_post == 'page') { // Do hierarchy $post['post_parent'] = $newarray[$i]['child_of_page_id']; } // GOGOGO $this_page_id = wp_insert_post($post); // Update $newarray[$i]['page_id'] = $this_page_id; $lastlevel = $level; $i++; } } else { // Post $i = 0; foreach ($titles as $title) { // Remove spaces before and after titles $title = trim($title); // No empty title? if (!empty ($title)) { $i++; // Create post object $post = array (); $post['post_title'] = $title; $post['post_type'] = $page_or_post; $post['post_content'] = ''; $post['post_status'] = 'publish'; $post['post_author'] = $author_id; // http://www.ramonfincken.com/permalink/topic184.html // Insert the post into the database wp_insert_post($post); } } } echo '
* Choose what you want to add: posts or pages
* Type the title of each post or page on a seperate line in the textarea
Optional:
* If it is a page, select the parent page (Default: none)