| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
function eshb_create_easy_hotel_pages() { |
| 4 |
|
| 5 |
// Define pages and templates |
| 6 |
$pages = [ |
| 7 |
'Easy Hotel Archive' => array('title' => 'Easy Hotel Archive', 'shortcode' => '[eshb_accomodation_grid]'), |
| 8 |
'Easy Hotel Search' => array('title' => 'Easy Hotel Search', 'shortcode' => '[eshb_search_form]'), |
| 9 |
'Easy Hotel Search Result' => array('title' => 'Easy Hotel Search Result', 'shortcode' => '[eshb_accomodation_search_result]'), |
| 10 |
]; |
| 11 |
|
| 12 |
foreach ($pages as $page_title => $page_data) { |
| 13 |
// Check if the page already exists using WP_Query |
| 14 |
$query = new WP_Query([ |
| 15 |
'post_type' => 'page', |
| 16 |
'title' => $page_title, |
| 17 |
'posts_per_page' => 1, // Only need one result |
| 18 |
]); |
| 19 |
|
| 20 |
// If no page found, create it |
| 21 |
if (!$query->have_posts()) { |
| 22 |
// Create the page with the shortcode in the content |
| 23 |
$page_id = wp_insert_post([ |
| 24 |
'post_title' => $page_data['title'], |
| 25 |
'post_content' => $page_data['shortcode'], // Add shortcode as content |
| 26 |
'post_status' => 'publish', |
| 27 |
'post_type' => 'page', |
| 28 |
]); |
| 29 |
|
| 30 |
// Check for errors in post creation |
| 31 |
if (is_wp_error($page_id)) { |
| 32 |
//error_log('Error creating page "' . $page_data['title'] . '": ' . $page_id->get_error_message()); |
| 33 |
} else { |
| 34 |
// Assign the page template |
| 35 |
update_post_meta($page_id, '_wp_page_template', $page_data['title']); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |