Advertisement
Guest User

Create WordPress Pages Programmatically

a guest
Sep 21st, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3. function your_theme_create_page($page_title, $page_content)
  4. {
  5.     $page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
  6.  
  7.     if ($page_obj) {
  8.  
  9.         return false;
  10.  
  11.         exit();
  12.  
  13.     }
  14.  
  15.     $page_args = array(
  16.  
  17.         'post_type'      => 'page',
  18.  
  19.         'post_status'    => 'publish',
  20.  
  21.         'post_title'     => ucwords($page_title),
  22.  
  23.         'post_name'      => strtolower(trim($page_title)),
  24.  
  25.         'post_content'   => $page_content,
  26.  
  27.     );
  28.  
  29.     $page_id = wp_insert_post($page_args);
  30.  
  31.     return $page_id;
  32.  
  33. }
  34. // Second Section of Code
  35. add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
  36.  
  37. function my_custom_pages_on_theme_activation()
  38. {
  39.  
  40.   $pages_array = array(
  41.     'Home' => '',    
  42.     'News' => '',
  43.     'Products' => '',
  44.     'Policy' => '',
  45.     'Services' => '',
  46.     'Tool Brands' => '',
  47.    
  48.     );
  49.  
  50.     foreach ($pages_array as $page_title => $page_content) {
  51.  
  52.         $current_page = your_theme_create_page($page_title, $page_content);
  53.  
  54.         if (false != $current_page) {
  55.  
  56.             add_action('admin_notices', function () use ($page_title) {
  57.  
  58. ?>
  59.  
  60.                 <div class="notice notice-success is-dismissible">
  61.  
  62.                     <p><?php echo "Done! {$page_title} has been created"; ?></p>
  63.  
  64.                 </div>
  65.  
  66.             <?php
  67.  
  68.             });
  69.  
  70.         } else {
  71.  
  72.             add_action('admin_notices', function () use ($page_title) {
  73.  
  74.             ?>
  75.  
  76.                 <div class="notice notice-warning is-dismissible">
  77.  
  78.                     <p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
  79.  
  80.                 </div>
  81.  
  82. <?php
  83.  
  84.             });
  85.  
  86.         }
  87.  
  88.     }
  89.  
  90.     $blog_page = get_page_by_title('news', 'OBJECT', 'page');
  91.  
  92.     if ($blog_page) {
  93.  
  94.         update_option('page_for_posts', $blog_page->ID);
  95.  
  96.     }
  97.  
  98.     $front_home_page = get_page_by_title('home', 'OBJECT', 'page');
  99.  
  100.     if ($front_home_page) {
  101.  
  102.         update_option('page_on_front', $front_home_page->ID);
  103.  
  104.         update_option('show_on_front', 'page');
  105.  
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement