Part 9 of 12 · Silver Rates Site

Making the Theme Build the Entire Site by Itself

By this stage, silverrates.today was thirty-two rate pages, a homepage, calculators, history pages, legal pages, menus, and settings, and I knew every step because I had done them by hand. Which meant a fresh install, or the forex site I was already planning, would cost a day of clicking. So the theme got an installer, activate it, and it builds the entire site. This post is that machinery, and the one property that makes site-builders safe, idempotence.

The installer runs on the theme activation hook and creates everything the site needs, pages first:

add_action('after_switch_theme', 'srt_install_site');

function srt_install_site() {
    $pages = array(
        'silver-rate-today' => array('Silver Rate Today', srt_build_hub_content('silver')),
        'gold-rate-today'   => array('Gold Rate Today',   srt_build_hub_content('gold')),
        'silver-calculator' => array('Silver Calculator', '[silver_calculator]'),
        'faq'               => array('FAQ', srt_build_faq_content()),
        'about-us'          => array('About Us', srt_page_copy('about')),
        'privacy-policy'    => array('Privacy Policy', srt_page_copy('privacy')),
        'disclaimer'        => array('Disclaimer', srt_page_copy('disclaimer')),
        // ...contact, terms, blog, history pages
    );
    foreach ($pages as $slug => $p) {
        if (!get_page_by_path($slug)) {          // the idempotence guard
            wp_insert_post(array(
                'post_type'   => 'page',
                'post_status' => 'publish',
                'post_name'   => $slug,
                'post_title'  => $p[0],
                'post_content'=> $p[1],
            ));
        }
    }
    srt_install_city_pages();   // the 30 city pages from the earlier post
    srt_install_menus();        // primary + footer menus pointing at the pages
}

The single most important line is the guard, get_page_by_path before wp_insert_post. Activation hooks fire again, theme updates, deactivate and reactivate, staging clones, and an installer without existence checks duplicates the whole site each time, which is precisely the duplicate-page disease I have fought elsewhere. Idempotent means running it twice changes nothing the second time, and every created thing follows the rule, pages checked by slug, menus checked by name before wp_create_nav_menu, menu items added only when the menu was just created, options seeded with add_option, which respects existing values, never update_option stomping the admin’s changes.

The content each page receives comes from the same builders the earlier posts developed, hub pages get their shortcode compositions, city pages their unique written content, legal pages real copy rather than lorem placeholders. The payoff arrived immediately and twice, a fresh staging copy of silverrates became a complete site in the time activation takes, and, more profitably, the whole installer pattern lifted straight into the forex project, where a site that took weeks the first time existed structurally in an afternoon. Automating your own setup feels like vanity right up until the second site, when it is the entire business case.

A few things people ask me about this

Why did my activation hook create duplicate pages? No existence guard. Activation fires more often than people expect, always check get_page_by_path, or a stored installed flag, before inserting.

add_option or update_option for installer defaults? add_option, it silently does nothing when the option exists, preserving the admin’s settings. update_option in an installer resets their choices on every reactivation.

Next

The theme built everything, including its own header and footer, and the day I designed a custom header in Elementor Pro, the site wore both at once. The two-headers bug is the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *