Halloween Sale 15% off Ends in See the plans
PHP

Copy Settings From a Template Site to New Sites

Copies chosen options, such as Customizer settings, the site icon and reading settings, from a template site onto every new site.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

$wpide_template_id = 0; // Blog ID of the template site (0 = off until you set it).
$wpide_options     = array( 'blogdescription', 'posts_per_page', 'date_format', 'time_format', 'timezone_string', 'permalink_structure', 'default_comment_status' );

add_action( 'wp_initialize_site', function ( $site ) use ( $wpide_template_id, $wpide_options ) {
    if ( ! $wpide_template_id || (int) $site->blog_id === (int) $wpide_template_id || ! get_site( $wpide_template_id ) ) {
        return;
    }

    switch_to_blog( $wpide_template_id );
    $values     = array();
    foreach ( $wpide_options as $option ) {
        $values[ $option ] = get_option( $option );
    }
    $stylesheet = get_option( 'stylesheet' );
    $template   = get_option( 'template' );
    $mods       = get_option( 'theme_mods_' . $stylesheet );
    restore_current_blog();

    switch_to_blog( $site->blog_id );
    foreach ( $values as $option => $value ) {
        if ( false !== $value ) {
            update_option( $option, $value );
        }
    }
    if ( $stylesheet && wp_get_theme( $stylesheet )->exists() ) {
        update_option( 'template', $template );
        update_option( 'stylesheet', $stylesheet );
        if ( is_array( $mods ) ) {
            unset( $mods['custom_logo'], $mods['nav_menu_locations'], $mods['custom_css_post_id'] );
            update_option( 'theme_mods_' . $stylesheet, $mods );
        }
    }
    delete_option( 'rewrite_rules' );
    restore_current_blog();
}, 220 );

Notes

Set $wpide_template_id to the blog ID of your template site. Options that point to posts or media (like page_on_front) are not copied because IDs differ between sites.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.