james-riter

Making Pages In WordPress

Aug 2nd, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. // Here are two codes I am trying to mix/ish
  2. // This code will make pages (as many as I need) when you active a theme in wordpress
  3. // How ever, it does NOT set  a template to use OR Content
  4.  
  5. function create_initial_pages() {
  6.     $pages = array(
  7.         'page1' => 'Page 1',
  8.         'page2' => 'Page 2',
  9.         'page3' => 'Page 3',
  10.         'page4' => 'Page 4'
  11.     );
  12.     foreach($pages as $page_url => $page_title) {
  13.         $id = get_page_by_title($page_title);
  14.         $page = array(
  15.             'post_type'   => 'page',
  16.         'post_name'   => $page_url,
  17.             'post_title'  => $page_title,
  18.             'post_status' => 'publish',
  19.             'post_author' => 1,
  20.             'post_parent' => ''
  21.         );
  22.         if (!isset($id)) wp_insert_post($page);
  23.     };
  24. }
  25.  
  26.  
  27. // This is code I have will only make ONE page but it will set some content and a template
  28. // that will be used when making the page...
  29.  
  30. if (isset($_GET['activated']) && is_admin()){
  31.     $new_page_title = 'Sitemap';
  32.     $new_page_content = ' ';
  33.     $new_page_template = 'sitemap.php'; //ex. template-custom.php. Leave blank if you don't want a custom page template.
  34.     //don't change the code bellow, unless you know what you're doing
  35.     $page_check = get_page_by_title($new_page_title);
  36.     $new_page = array(
  37.         'post_type' => 'page',
  38.         'post_title' => $new_page_title,
  39.         'post_content' => $new_page_content,
  40.         'post_status' => 'publish',
  41.         'post_author' => 1,
  42.     );
  43.     if(!isset($page_check->ID)){
  44.         $new_page_id = wp_insert_post($new_page);
  45.         if(!empty($new_page_template)){
  46.             update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
  47.         }
  48.     }
  49. }
  50.  
  51. ///  Both codes work, but I need code that will make more than one page and set everything. . .
  52. /// I think this would word if I were to use a 2 dimensional array but my coding is not that good.. . .
  53.  
  54. // Here is a bad a temp to show what I am trying to do with code. . .  
  55. // I did comment out my bad coding to make it so you could see it
  56.  
  57.  
  58.  
  59. if (isset($_GET['activated']) && is_admin()){
  60.     add_action('init', 'create_initial_pages');
  61. }
  62. function create_initial_pages() {
  63.     $pages = array(
  64.         'page1' => 'Page 1', // => 'Content Coming Soon', => 'custom-file.php',
  65.         'page2' => 'Page 2', // => 'Content Coming Soon', => 'custom-file.php',
  66.         'page3' => 'Page 3', // => 'Content Coming Soon', => 'custom-file.php',
  67.         'page4' => 'Page 4'  // => 'Content Coming Soon', => 'custom-file.php',
  68.     );
  69.     foreach($pages as $page_url => $page_title /** => $coming_content => $cuustom_file **/ ) {
  70.         $id = get_page_by_title($page_title);
  71.         $page = array(
  72.             'post_type'   =>  'page',
  73.             'post_name'   =>  $page_url,
  74.             'post_title'  =>  $page_title,
  75.             'post_content' => $page_content,
  76.             'post_status' =>  'publish',
  77.             'post_author' =>  1,
  78.             'post_parent' =>  ''
  79.         );
  80.         if (!isset($id)) wp_insert_post($page);
  81.        
  82.         if(!isset($id->ID)){
  83.         $new_page_id = wp_insert_post($new);
  84.         if(!empty($cuustom_file)){
  85.             update_post_meta($new_page_id, '_wp_page_template', $cuustom_file);
  86.         }
  87.     }
  88.        
  89.        
  90.        
  91.     };
  92. }
  93. $page_check = get_page_by_title($new_page_title);
Advertisement
Add Comment
Please, Sign In to add comment