array('title'=>'Home','content'=> $basecraft_dummy_text,'option_page'=>'home'), 'about-us'=>array('title'=>'About Us','content'=> $basecraft_dummy_text,'option_page'=>'about_us'), 'contact-us'=>array('title'=>'Contact Us','content'=>$basecraft_dummy_text,'option_page'=>'contact_us'), ); foreach($basecraft_pages as $basecraft_key => $basecraft_page_value) { if(null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = '".$basecraft_key."'")) { $basecraft_current_user = wp_get_current_user(); // CREATE POST OBJECT $basecraft_page = array( 'post_title' => $basecraft_page_value['title'], 'post_status' => 'publish', 'post_content'=> $basecraft_page_value['content'], 'post_author' => $basecraft_current_user->ID, 'post_type' => 'page', ); // CHECK IF PAGE NOT EXISTS if(!get_page_by_path( $basecraft_page_value['title'], OBJECT, 'page')) { // INSERT THE POST INTO THE DATABASE $basecraft_post_id = wp_insert_post( $basecraft_page ); update_option($basecraft_page_value['option_page'],$basecraft_post_id); } } } //END - CREATE PAGES BY CODE //CREATE CATEGORY BY CODE if(file_exists (ABSPATH.'/wp-admin/includes/taxonomy.php')) { require_once (ABSPATH.'/wp-admin/includes/taxonomy.php'); if(! get_cat_ID( 'Blog')) { wp_create_category( 'Blog' ); } $basecraft_blog_category_id = get_cat_ID( 'Blog' ); } //END - CREATE CATEGORY BY CODE // CREATE, POST BY CODE $basecraft_default_posts = array( 'lorem_ipsum_1' => array('title' => 'Lorem Ipsum 1', 'content' => $basecraft_dummy_text), 'lorem_ipsum_2' => array('title' => 'Lorem Ipsum 2', 'content' => $basecraft_dummy_text), 'lorem_ipsum_3' => array('title' => 'Lorem Ipsum 3', 'content' => $basecraft_dummy_text), 'lorem_ipsum_4' => array('title' => 'Lorem Ipsum 4', 'content' => $basecraft_dummy_text), 'lorem_ipsum_5' => array('title' => 'Lorem Ipsum 5', 'content' => $basecraft_dummy_text), 'lorem_ipsum_6' => array('title' => 'Lorem Ipsum 6', 'content' => $basecraft_dummy_text), ); // CHECK IF THE PLACEHOLDER IMAGE HAS BEEN UPLOADED $basecraft_attachment_id = get_transient('basecraft_uploaded_image_placeholder'); if (!$basecraft_attachment_id) { $basecraft_placeholder_image = get_template_directory() . '/assets/images/basecraft-placeholder-image.webp'; $basecraft_image_data = file_get_contents($basecraft_placeholder_image); $basecraft_upload_dir = wp_upload_dir(); $basecraft_filename = basename($basecraft_placeholder_image); // SAFELY UPLOAD THE FILE $basecraft_upload_file = wp_upload_bits($basecraft_filename, null, $basecraft_image_data); if (!$basecraft_upload_file['error']) { $basecraft_attachment = array( 'guid' => $basecraft_upload_file['url'], 'post_mime_type' => 'image/webp', 'post_title' => 'Placeholder Image', 'post_content' => '', 'post_status' => 'inherit' ); $basecraft_attachment_id = wp_insert_attachment($basecraft_attachment, $basecraft_upload_file['file']); // METADATA FOR THE ATTACHMENT require_once(ABSPATH . 'wp-admin/includes/image.php'); $basecraft_attachment_data = wp_generate_attachment_metadata($basecraft_attachment_id, $basecraft_upload_file['file']); wp_update_attachment_metadata($basecraft_attachment_id, $basecraft_attachment_data); // STORE THE IMAGE ID set_transient( 'basecraft_uploaded_image_placeholder', $basecraft_attachment_id, 0 ); } } foreach ($basecraft_default_posts as $basecraft_key => $basecraft_post_value) { if (null === $wpdb->get_row("SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = '" . $basecraft_key . "'")) { $basecraft_current_user = wp_get_current_user(); $basecraft_dummy_post_add = array( 'post_title' => $basecraft_post_value['title'], 'post_status' => 'publish', 'post_content' => $basecraft_post_value['content'], 'post_category' => array($basecraft_blog_category_id), 'post_author' => $basecraft_current_user->ID, 'post_type' => 'post' ); if (!get_page_by_path($basecraft_post_value['title'], OBJECT, 'post')) { $basecraft_post_id = wp_insert_post($basecraft_dummy_post_add); if ($basecraft_post_id && !has_post_thumbnail($basecraft_post_id)) { set_post_thumbnail($basecraft_post_id, $basecraft_attachment_id); } } } } // END, CREATE POST CODE // CREATE HEADER AND FOOTER MENUS $basecraft_locations = get_theme_mod('nav_menu_locations'); // DEFINE MENU LOCATION // GET THE PAGE IDS $basecraft_home_id = 0; $basecraft_home_query = new WP_Query(array( 'post_type' => 'page', 'title' => 'Home', 'posts_per_page' => 1, )); if ($basecraft_home_query->have_posts()) { $basecraft_home_query->the_post(); $basecraft_home_id = get_the_ID(); } wp_reset_postdata(); $basecraft_about_us_id = get_option('about_us'); $basecraft_contact_us_id = get_option('contact_us'); // FUNCTION TO GET OR CREATE A MENU function basecraft_get_or_create_menu($basecraft_menu_name) { $basecraft_menu_exists = wp_get_nav_menu_object($basecraft_menu_name); if ($basecraft_menu_exists) { return $basecraft_menu_exists->term_id; } return wp_create_nav_menu($basecraft_menu_name); } // GET OR CREATE HEADER AND FOOTER MENUS $basecraft_header_menu_id = basecraft_get_or_create_menu('Header Menu'); $basecraft_footer_menu_id = basecraft_get_or_create_menu('Footer Menu'); // FUNCTION TO ADD A MENU ITEM IF IT DOES NOT ALREADY EXISTS function basecraft_add_menu_item_if_not_exists($basecraft_menu_id, $basecraft_menu_item_title, $basecraft_menu_item_object_id) { if (!$basecraft_menu_item_object_id) return; $basecraft_menu_items = wp_get_nav_menu_items($basecraft_menu_id); $basecraft_exists = false; if ($basecraft_menu_items) { foreach ($basecraft_menu_items as $basecraft_menu_item) { if ($basecraft_menu_item->object_id == $basecraft_menu_item_object_id) { $basecraft_exists = true; break; } } } if (!$basecraft_exists) { wp_update_nav_menu_item($basecraft_menu_id, 0, array( 'menu-item-title' => $basecraft_menu_item_title, 'menu-item-object-id' => $basecraft_menu_item_object_id, 'menu-item-object' => 'page', 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish' )); } } // ADD MENU ITEMS IF THEY DON'T ALREADY EXISTS basecraft_add_menu_item_if_not_exists($basecraft_header_menu_id, 'Home', $basecraft_home_id); basecraft_add_menu_item_if_not_exists($basecraft_header_menu_id, 'About Us', $basecraft_about_us_id); basecraft_add_menu_item_if_not_exists($basecraft_header_menu_id, 'Contact Us', $basecraft_contact_us_id); basecraft_add_menu_item_if_not_exists($basecraft_footer_menu_id, 'Home', $basecraft_home_id); basecraft_add_menu_item_if_not_exists($basecraft_footer_menu_id, 'About Us', $basecraft_about_us_id); basecraft_add_menu_item_if_not_exists($basecraft_footer_menu_id, 'Contact Us', $basecraft_contact_us_id); // ASSIGN MENU TO THEME LOCATIONS $basecraft_locations['primary'] = $basecraft_header_menu_id; $basecraft_locations['footer'] = $basecraft_footer_menu_id; set_theme_mod('nav_menu_locations', $basecraft_locations); // END, CREATE HEADER AND FOOTER MENUS // START - UPLOAD LOGO IMAGE ON THEME ACTIVATION $basecraft_dummy_logo = get_template_directory() . '/assets/images/basecraft-dummy-logo.png'; if (!$basecraft_logo_attachment_id && file_exists($basecraft_dummy_logo)) { $basecraft_image_data = file_get_contents($basecraft_dummy_logo); $basecraft_upload_dir = wp_upload_dir(); $basecraft_filename = basename($basecraft_dummy_logo); $basecraft_upload_file = wp_upload_bits($basecraft_filename, null, $basecraft_image_data); if (!$basecraft_upload_file['error']) { $basecraft_attachment = array( 'guid' => $basecraft_upload_file['url'], 'post_mime_type' => 'image/png', 'post_title' => 'Dummy Logo', 'post_content' => '', 'post_status' => 'inherit', ); $basecraft_logo_attachment_id = wp_insert_attachment($basecraft_attachment, $basecraft_upload_file['file']); if ($basecraft_logo_attachment_id) { require_once(ABSPATH . 'wp-admin/includes/image.php'); $basecraft_attachment_data = wp_generate_attachment_metadata($basecraft_logo_attachment_id, $basecraft_upload_file['file']); wp_update_attachment_metadata($basecraft_logo_attachment_id, $basecraft_attachment_data); set_transient('basecraft_uploaded_image_id_logo', $basecraft_logo_attachment_id, 0); } } } // END - UPLOAD LOGO IMAGE ON THEME ACTIVATION // SET POST NAME AS A PERMALINK global $wp_rewrite; $wp_rewrite->set_permalink_structure('/%postname%/'); update_option( "rewrite_rules", FALSE ); $wp_rewrite->flush_rules( true ); // END, SET POST NAME AS A PERMALINK// CREATE PAGES BY CODE