Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- function your_theme_create_page($page_title, $page_content)
- {
- $page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
- if ($page_obj) {
- return false;
- exit();
- }
- $page_args = array(
- 'post_type' => 'page',
- 'post_status' => 'publish',
- 'post_title' => ucwords($page_title),
- 'post_name' => strtolower(trim($page_title)),
- 'post_content' => $page_content,
- );
- $page_id = wp_insert_post($page_args);
- return $page_id;
- }
- // Second Section of Code
- add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
- function my_custom_pages_on_theme_activation()
- {
- $pages_array = array(
- 'Home' => '',
- 'News' => '',
- 'Products' => '',
- 'Policy' => '',
- 'Services' => '',
- 'Tool Brands' => '',
- );
- foreach ($pages_array as $page_title => $page_content) {
- $current_page = your_theme_create_page($page_title, $page_content);
- if (false != $current_page) {
- add_action('admin_notices', function () use ($page_title) {
- ?>
- <div class="notice notice-success is-dismissible">
- <p><?php echo "Done! {$page_title} has been created"; ?></p>
- </div>
- <?php
- });
- } else {
- add_action('admin_notices', function () use ($page_title) {
- ?>
- <div class="notice notice-warning is-dismissible">
- <p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
- </div>
- <?php
- });
- }
- }
- $blog_page = get_page_by_title('news', 'OBJECT', 'page');
- if ($blog_page) {
- update_option('page_for_posts', $blog_page->ID);
- }
- $front_home_page = get_page_by_title('home', 'OBJECT', 'page');
- if ($front_home_page) {
- update_option('page_on_front', $front_home_page->ID);
- update_option('show_on_front', 'page');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement