Gepubliseer op Maak 'n opmerking

Rusk Starter Content Importer

What this will create

  • Pages: Home, About, Contact (and Shop if WooCommerce exists)
  • Sets Home as the static front page
  • Primary menu (or the first available menu location)
  • Optional: WooCommerce categories and sample products (if WooCommerce is installed)
  • Optional: Imports images from /assets and assigns them


Notes

  • If items already exist (same slug), the importer will not duplicate them.
  • To change text later, edit Pages under Pages and Products under Products.
[], ‘menu’ => null, ‘front_page’ => null, ‘images’ => [], ‘products’ => [], ]; // 1) Create pages $pages = [ ‘home’ => [ ‘title’ => ‘Home’, ‘slug’ => ‘home’, ‘content’ => $this->home_content(), ], ‘about’ => [ ‘title’ => ‘About’, ‘slug’ => ‘about’, ‘content’ => $this->about_content(), ], ‘contact’ => [ ‘title’ => ‘Contact’, ‘slug’ => ‘contact’, ‘content’ => $this->contact_content(), ], ]; foreach ($pages as $key => $p) { $id = $this->create_page_if_missing($p[‘title’], $p[‘slug’], $p[‘content’]); $result[‘pages’][$key] = $id; } // 2) Set front page to Home if (!empty($result[‘pages’][‘home’])) { update_option(‘show_on_front’, ‘page’); update_option(‘page_on_front’, (int) $result[‘pages’][‘home’]); $result[‘front_page’] = (int) $result[‘pages’][‘home’]; } // 3) WooCommerce Shop page link (if WC present) if (class_exists(‘WooCommerce’)) { $shop_id = wc_get_page_id(‘shop’); if ($shop_id && $shop_id > 0) { $result[‘pages’][‘shop’] = $shop_id; } else { // create a normal page called Shop if WC didn’t create it yet $shop_id = $this->create_page_if_missing(‘Shop’, ‘shop’, ‘[products limit=”12″ columns=”3″]’); $result[‘pages’][‘shop’] = $shop_id; } } // 4) Import images from /assets (optional) $attachment_ids = []; if ($import_images) { $attachment_ids = $this->import_assets_images(); $result[‘images’] = $attachment_ids; } // 5) Create menu $menu_id = $this->create_menu_if_missing(‘Main Menu’); $result[‘menu’] = $menu_id; // Add menu items (skip if already present) $this->add_menu_item_page($menu_id, ‘Home’, $result[‘pages’][‘home’] ?? 0); $this->add_menu_item_page($menu_id, ‘About’, $result[‘pages’][‘about’] ?? 0); $this->add_menu_item_page($menu_id, ‘Contact’, $result[‘pages’][‘contact’] ?? 0); if (!empty($result[‘pages’][‘shop’])) { $this->add_menu_item_page($menu_id, ‘Shop’, $result[‘pages’][‘shop’]); } // Assign menu to a location (primary if exists, else first location) $this->assign_menu_to_location($menu_id); // 6) Create WooCommerce products (optional) if ($import_products && class_exists(‘WooCommerce’)) { $result[‘products’] = $this->create_sample_products($attachment_ids); } update_option(self::OPTION_IMPORTED, time()); wp_safe_redirect(admin_url(‘tools.php?page=rusk-starter-importer&import=done’)); exit; } /* ——————————- * Content blocks * ——————————- */ private function home_content() { // If the hero image exists in Media Library, you can replace with a real hero block later. return <<Handmade Rusks, Baked with Love

Fresh, crunchy rusks — perfect with coffee or tea. Order online for delivery or collection.


Popular Rusks

Try our best sellers: Buttermilk, Aniseed, Bran, and Chocolate Chip.

[products limit=”4″ columns=”4″ orderby=”popularity”]

HTML; } private function about_content() { return <<About Corliese Hoekie

We bake small-batch rusks using traditional recipes and quality ingredients. Our rusks are dried to a perfect crunch, packed fresh, and shipped with care.

Made in South Africa. Perfect for gifting, events, guesthouses, or everyday coffee moments.

HTML; } private function contact_content() { return <<Contact / Orders

For bulk orders, custom packs, or delivery questions, get in touch:

You can also place orders directly through the Shop page.

HTML; } /* ——————————- * Helpers: create pages / menu * ——————————- */ private function create_page_if_missing($title, $slug, $content) { $existing = get_page_by_path($slug); if ($existing && isset($existing->ID)) { // Update content if empty only if (empty(trim($existing->post_content))) { wp_update_post([ ‘ID’ => $existing->ID, ‘post_content’ => $content ]); } return (int) $existing->ID; } $id = wp_insert_post([ ‘post_title’ => $title, ‘post_name’ => $slug, ‘post_content’ => $content, ‘post_status’ => ‘publish’, ‘post_type’ => ‘page’, ]); return is_wp_error($id) ? 0 : (int) $id; } private function create_menu_if_missing($menu_name) { $menu = wp_get_nav_menu_object($menu_name); if ($menu && isset($menu->term_id)) return (int) $menu->term_id; $menu_id = wp_create_nav_menu($menu_name); return is_wp_error($menu_id) ? 0 : (int) $menu_id; } private function add_menu_item_page($menu_id, $label, $page_id) { if (!$menu_id || !$page_id) return; $items = wp_get_nav_menu_items($menu_id); if ($items) { foreach ($items as $item) { if ((int)$item->object_id === (int)$page_id) { return; // already exists } } } wp_update_nav_menu_item($menu_id, 0, [ ‘menu-item-title’ => $label, ‘menu-item-object’ => ‘page’, ‘menu-item-object-id’ => (int) $page_id, ‘menu-item-type’ => ‘post_type’, ‘menu-item-status’ => ‘publish’ ]); } private function assign_menu_to_location($menu_id) { if (!$menu_id) return; $locations = get_theme_mod(‘nav_menu_locations’, []); $registered = get_registered_nav_menus(); // Prefer “primary”, else first location $target = null; if (isset($registered[‘primary’])) { $target = ‘primary’; } elseif (!empty($registered)) { $keys = array_keys($registered); $target = $keys[0]; } if ($target) { $locations[$target] = $menu_id; set_theme_mod(‘nav_menu_locations’, $locations); } } /* ——————————- * Image import from plugin assets * ——————————- */ private function import_assets_images() { $map = []; $files = [ ‘hero.jpg’ => ‘Hero Image’, ‘buttermilk-rusks.jpg’ => ‘Buttermilk Rusks’, ‘aniseed-rusks.jpg’ => ‘Aniseed Rusks’, ‘bran-rusks.jpg’ => ‘Bran Rusks’, ‘chocchip-rusks.jpg’ => ‘Chocolate Chip Rusks’, ]; require_once ABSPATH . ‘wp-admin/includes/file.php’; require_once ABSPATH . ‘wp-admin/includes/media.php’; require_once ABSPATH . ‘wp-admin/includes/image.php’; foreach ($files as $filename => $title) { $path = plugin_dir_path(__FILE__) . ‘assets/’ . $filename; if (!file_exists($path)) continue; // Skip if an attachment already exists with same file name in uploads $existing_id = $this->find_attachment_by_filename($filename); if ($existing_id) { $map[$filename] = $existing_id; continue; } $upload = wp_upload_bits($filename, null, file_get_contents($path)); if (!empty($upload[‘error’])) continue; $filetype = wp_check_filetype($upload[‘file’], null); $attachment_id = wp_insert_attachment([ ‘post_mime_type’ => $filetype[‘type’], ‘post_title’ => $title, ‘post_content’ => ”, ‘post_status’ => ‘inherit’ ], $upload[‘file’]); if (is_wp_error($attachment_id)) continue; $attach_data = wp_generate_attachment_metadata($attachment_id, $upload[‘file’]); wp_update_attachment_metadata($attachment_id, $attach_data); $map[$filename] = (int) $attachment_id; } return $map; // filename => attachment_id } private function find_attachment_by_filename($filename) { global $wpdb; $like = ‘%’ . $wpdb->esc_like(‘/’ . $filename) . ‘%’; $id = $wpdb->get_var($wpdb->prepare( “SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = ‘_wp_attached_file’ AND meta_value LIKE %s LIMIT 1”, $like )); return $id ? (int) $id : 0; } /* ——————————- * WooCommerce products * ——————————- */ private function create_sample_products($attachments_map) { if (!class_exists(‘WC_Product_Simple’)) return []; // Create categories $cats = [‘Buttermilk’, ‘Aniseed’, ‘Bran’, ‘Sweet’]; foreach ($cats as $cat) { if (!term_exists($cat, ‘product_cat’)) { wp_insert_term($cat, ‘product_cat’); } } $products = [ [ ‘sku’ => ‘RUSK-BUT-001’, ‘name’ => ‘Buttermilk Rusks (1kg)’, ‘price’ => 95, ‘category’ => ‘Buttermilk’, ‘image’ => ‘buttermilk-rusks.jpg’, ‘desc’ => ‘Crisp golden buttermilk rusks baked in small batches. Perfect with coffee or tea.’, ], [ ‘sku’ => ‘RUSK-ANI-001’, ‘name’ => ‘Aniseed Rusks (1kg)’, ‘price’ => 100, ‘category’ => ‘Aniseed’, ‘image’ => ‘aniseed-rusks.jpg’, ‘desc’ => ‘Traditional aniseed flavour with a satisfying crunch.’, ], [ ‘sku’ => ‘RUSK-BRA-001’, ‘name’ => ‘Bran Rusks (1kg)’, ‘price’ => 99, ‘category’ => ‘Bran’, ‘image’ => ‘bran-rusks.jpg’, ‘desc’ => ‘Wholesome high-fibre bran rusks with a hearty bite.’, ], [ ‘sku’ => ‘RUSK-CHO-001’, ‘name’ => ‘Chocolate Chip Rusks (1kg)’, ‘price’ => 120, ‘sale_price’ => 110, ‘category’ => ‘Sweet’, ‘image’ => ‘chocchip-rusks.jpg’, ‘desc’ => ‘A sweeter rusk with chocolate chips — a crowd favourite.’, ], ]; $created = []; foreach ($products as $p) { // Skip if SKU exists $existing_id = wc_get_product_id_by_sku($p[‘sku’]); if ($existing_id) { $created[] = (int) $existing_id; continue; } $product = new WC_Product_Simple(); $product->set_name($p[‘name’]); $product->set_sku($p[‘sku’]); $product->set_status(‘publish’); $product->set_catalog_visibility(‘visible’); $product->set_regular_price((string) $p[‘price’]); if (!empty($p[‘sale_price’])) { $product->set_sale_price((string) $p[‘sale_price’]); } $product->set_description($p[‘desc’]); $product->set_short_description($p[‘desc’]); $product->set_weight(‘1’); // 1kg default // Category $term = get_term_by(‘name’, $p[‘category’], ‘product_cat’); if ($term && !is_wp_error($term)) { $product->set_category_ids([(int) $term->term_id]); } // Image if (!empty($p[‘image’]) && !empty($attachments_map[$p[‘image’]])) { $product->set_image_id((int) $attachments_map[$p[‘image’]]); } $id = $product->save(); if ($id) $created[] = (int) $id; } return $created; } } new Rusk_Starter_Importer();