array('title'=>'Home','content'=> $dummy_text,'option_page'=>'home'), 'about-us'=>array('title'=>'About Us','content'=> $dummy_text,'option_page'=>'about_us'), 'contact-us'=>array('title'=>'Contact Us','content'=>$dummy_text,'option_page'=>'contact_us'), ); foreach($pages as $key => $page_value) { if(null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = '".$key."'")) { $current_user = wp_get_current_user(); // CREATE POST OBJECT $page = array( 'post_title' => $page_value['title'], 'post_status' => 'publish', 'post_content'=> $page_value['content'], 'post_author' => $current_user->ID, 'post_type' => 'page', ); // CHECK IF PAGE NOT EXISTS if(!get_page_by_path( $page_value['title'], OBJECT, 'page')) { // INSERT THE POST INTO THE DATABASE $postID = wp_insert_post( $page ); update_option($page_value['option_page'],$postID); } } } //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' ); } $blog_category_id = get_cat_ID( 'Blog' ); } //END - CREATE CATEGORY BY CODE // CREATE, POST BY CODE $default_posts = array( 'lorem_ipsum_1' => array('title' => 'Lorem Ipsum 1', 'content' => $dummy_text), 'lorem_ipsum_2' => array('title' => 'Lorem Ipsum 2', 'content' => $dummy_text), 'lorem_ipsum_3' => array('title' => 'Lorem Ipsum 3', 'content' => $dummy_text), 'lorem_ipsum_4' => array('title' => 'Lorem Ipsum 4', 'content' => $dummy_text), 'lorem_ipsum_5' => array('title' => 'Lorem Ipsum 5', 'content' => $dummy_text), 'lorem_ipsum_6' => array('title' => 'Lorem Ipsum 6', 'content' => $dummy_text), ); // CHECK IF THE PLACEHOLDER IMAGE HAS BEEN UPLOADED $attachment_id = get_transient('uploaded_image_placeholder'); if (!$attachment_id) { $placeholder_image = get_template_directory() . '/assets/images/placeholder-image.webp'; $image_data = file_get_contents($placeholder_image); $upload_dir = wp_upload_dir(); $filename = basename($placeholder_image); // SAFELY UPLOAD THE FILE $upload_file = wp_upload_bits($filename, null, $image_data); if (!$upload_file['error']) { $attachment = array( 'guid' => $upload_file['url'], 'post_mime_type' => 'image/webp', 'post_title' => 'Placeholder Image', 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment($attachment, $upload_file['file']); // METADATA FOR THE ATTACHMENT require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); wp_update_attachment_metadata($attachment_id, $attachment_data); // STORE THE IMAGE ID set_transient( 'uploaded_image_placeholder', $attachment_id, 0 ); } } foreach ($default_posts as $key => $post_value) { if (null === $wpdb->get_row("SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = '" . $key . "'")) { $current_user = wp_get_current_user(); $dummy_post_add = array( 'post_title' => $post_value['title'], 'post_status' => 'publish', 'post_content' => $post_value['content'], 'post_category' => array($blog_category_id), 'post_author' => $current_user->ID, 'post_type' => 'post' ); if (!get_page_by_path($post_value['title'], OBJECT, 'post')) { $post_id = wp_insert_post($dummy_post_add); if ($post_id && !has_post_thumbnail($post_id)) { set_post_thumbnail($post_id, $attachment_id); } } } } // END, CREATE POST CODE // CREATE HEADER AND FOOTER MENUS $locations = get_theme_mod('nav_menu_locations'); // DEFINE MENU LOCATION // GET THE PAGE IDS $home_page = get_page_by_title('Home'); $home_id = $home_page ? $home_page->ID : 0; $about_us_id = get_option('about_us'); $contact_us_id = get_option('contact_us'); // FUNCTION TO GET OR CREATE A MENU function get_or_create_menu($menu_name) { $menu_exists = wp_get_nav_menu_object($menu_name); if ($menu_exists) { return $menu_exists->term_id; } return wp_create_nav_menu($menu_name); } // GET OR CREATE HEADER AND FOOTER MENUS $header_menu_id = get_or_create_menu('Header Menu'); $footer_menu_id = get_or_create_menu('Footer Menu'); // FUNCTION TO ADD A MENU ITEM IF IT DOES NOT ALREADY EXISTS function add_menu_item_if_not_exists($menu_id, $menu_item_title, $menu_item_object_id) { if (!$menu_item_object_id) return; $menu_items = wp_get_nav_menu_items($menu_id); $exists = false; if ($menu_items) { foreach ($menu_items as $menu_item) { if ($menu_item->object_id == $menu_item_object_id) { $exists = true; break; } } } if (!$exists) { wp_update_nav_menu_item($menu_id, 0, array( 'menu-item-title' => $menu_item_title, 'menu-item-object-id' => $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 add_menu_item_if_not_exists($header_menu_id, 'Home', $home_id); add_menu_item_if_not_exists($header_menu_id, 'About Us', $about_us_id); add_menu_item_if_not_exists($header_menu_id, 'Contact Us', $contact_us_id); add_menu_item_if_not_exists($footer_menu_id, 'Home', $home_id); add_menu_item_if_not_exists($footer_menu_id, 'About Us', $about_us_id); add_menu_item_if_not_exists($footer_menu_id, 'Contact Us', $contact_us_id); // ASSIGN MENU TO THEME LOCATIONS $locations['primary'] = $header_menu_id; $locations['footer'] = $footer_menu_id; set_theme_mod('nav_menu_locations', $locations); // END, CREATE HEADER AND FOOTER MENUS // ADD DEFAULT PLACEHOLDER IMAGE AND TEXT FOR THEME SETTINGS $favicon = get_template_directory_uri() . '/assets/images/fevicon.ico'; $dummy_logo = get_template_directory_uri() . '/assets/images/dummy-logo.svg'; $banner_image = get_template_directory_uri() . '/assets/images/placeholder-image.webp'; if (class_exists('acf')) { $uploaded_image_placeholder = get_transient('uploaded_image_placeholder'); // ID $uploaded_image_id_logo = get_transient('uploaded_image_id_logo'); // ID $acf_fields = [ 'site_header_logo' => [ 'ID' => $uploaded_image_id_logo, 'url' => wp_get_attachment_url($uploaded_image_id_logo), 'alt' => get_post_meta($uploaded_image_id_logo, '_wp_attachment_image_alt', true), 'title' => get_the_title($uploaded_image_id_logo), ], 'site_footer_logo' => [ 'ID' => $uploaded_image_id_logo, 'url' => wp_get_attachment_url($uploaded_image_id_logo), 'alt' => get_post_meta($uploaded_image_id_logo, '_wp_attachment_image_alt', true), 'title' => get_the_title($uploaded_image_id_logo), ], 'banner_image' => [ 'ID' => $uploaded_image_placeholder, 'url' => wp_get_attachment_url($uploaded_image_placeholder), 'alt' => get_post_meta($uploaded_image_placeholder, '_wp_attachment_image_alt', true), 'title' => get_the_title($uploaded_image_placeholder), ], 'primary_color' => '#FFFFFF', 'secondary_color' => '#000000', 'body_text_color' => '#000000', 'button_background_color' => '#000000', 'button_hover_background_color' => '#FFFFFF', 'copyright_text' => '© Lorem Ipsum', 'main_title' => 'Sorry, Not Found', 'notfound_text' => '404', 'button_text' => [ 'title' => 'Home', 'url' => home_url('/'), 'target' => '_self' ], ]; foreach ($acf_fields as $field => $default_value) { if (!get_field($field, 'option')) { update_field($field, $default_value, 'option'); } else { update_field($field, $default_value, 'option'); } } } else { $meta_fields = [ 'site_favicon' => $favicon, 'site_header_logo' => $dummy_logo, 'site_footer_logo' => $dummy_logo, 'copyright_text' => '© Lorem Ipsum', 'banner_image' => $banner_image, 'main_title' => 'Sorry, Not Found', 'notfound_text' => '404', 'button_url' => home_url(), 'button_text' => 'Home', ]; foreach ($meta_fields as $field => $default_value) { if (!get_option($field)) { update_option($field, $default_value); } } } // END, ADD DEFAULT PLACEHOLDER IMAGE AND TEXT FOR THEME SETTINGS // ADD DEFAULT SOCIAL MEDIA ROWS $facebook = get_template_directory_uri() . '/assets/images/facebook.svg'; $instagram = get_template_directory_uri() . '/assets/images/instagram.svg'; $twitter = get_template_directory_uri() . '/assets/images/twitter.svg'; $default_social_media = array( array('link' => 'https://www.facebook.com/', 'logo' => $facebook), array('link' => 'https://www.instagram.com/', 'logo' => $instagram), array('link' => 'https://x.com/', 'logo' => $twitter), ); if (!get_option('social_media')) { update_option('social_media', $default_social_media); } // END, ADD DEFAULT SOCIAL MEDIA ROWS // SET DEFAULT COLOR OPTIONS $default_colors = array( 'primary_color' => '#ffffff', 'secondary_color' => '#000000', 'body_text_color' => '#000000', 'button_background_color' => '#000000', 'button_hover_background_color' => '#ffffff', ); foreach ($default_colors as $option_name => $default_value) { if (!get_option($option_name)) { update_option($option_name, $default_value); } } // END, SET DEFAULT COLOR OPTIONS // START - UPLOAD LOGO IMAGE ON THEME ACTIVATION $logo_attachment_id = get_transient('uploaded_image_id_logo'); if (!$logo_attachment_id) { $image_data = file_get_contents($dummy_logo); $upload_dir = wp_upload_dir(); $filename = basename($dummy_logo); // SAFELY UPLOAD THE FILE $upload_file = wp_upload_bits($filename, null, $image_data); if (!$upload_file['error']) { $attachment = array( 'guid' => $upload_file['url'], 'post_mime_type' => 'image/svg+xml', 'post_title' => 'Dummy Logo', 'post_content' => '', 'post_status' => 'inherit', ); $logo_attachment_id = wp_insert_attachment($attachment, $upload_file['file']); if ($logo_attachment_id) { require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_data = wp_generate_attachment_metadata($logo_attachment_id, $upload_file['file']); wp_update_attachment_metadata($logo_attachment_id, $attachment_data); set_transient('uploaded_image_id_logo', $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