You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress多站点如何自定义新站点默认设置?

How to Set Default Configurations for New WordPress Multisite Sites

Absolutely—creating a Must-Use (MU) Plugin is the cleanest, most reliable way to enforce default settings for new sites in your WordPress multisite network. MU plugins load automatically across all sites in your network, making them perfect for this kind of global configuration. Let’s walk through how to implement both of your requirements:

1. Auto-Enable Child Theme & Set Default Homepage

We’ll use the wpmu_new_blog hook, which fires immediately after a new site is created. This lets us switch to the new site’s context and apply our theme and homepage settings.

Add this code to a new file (e.g., multisite-defaults.php) in your wp-content/mu-plugins/ directory:

add_action( 'wpmu_new_blog', 'set_default_multisite_theme_homepage', 10, 6 );
function set_default_multisite_theme_homepage( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
    // Switch to the newly created blog
    switch_to_blog( $blog_id );

    // Replace 'child-theme-slug' with your child theme's actual slug
    $child_theme_slug = 'child-theme-slug';
    switch_theme( $child_theme_slug );

    // Create a default homepage (skip this if you want to use an existing template)
    $homepage_id = wp_insert_post( array(
        'post_title'     => 'Home',
        'post_content'   => '', // Add default content here if needed
        'post_status'    => 'publish',
        'post_type'      => 'page',
        'post_author'    => $user_id,
        'comment_status' => 'closed',
        'ping_status'    => 'closed'
    ) );

    // Set the new page as the homepage
    update_option( 'show_on_front', 'page' );
    update_option( 'page_on_front', $homepage_id );

    // Switch back to the main site
    restore_current_blog();
}

Note: Make sure to replace child-theme-slug with the actual slug of your installed child theme (you can find this in the theme’s style.css file, next to Theme Slug).

2. Assign "Designer" User Group as Site Administrators

We’ll extend the same wpmu_new_blog hook to add all users in your "Designer" user group as administrators to the new site. First, confirm the slug of your user group (adjust if you’re using a custom role system instead of default WordPress roles).

Add this code to the same MU plugin file:

add_action( 'wpmu_new_blog', 'add_designers_as_administrators', 10, 6 );
function add_designers_as_administrators( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
    // Replace 'designer' with your user group/role slug
    $designer_role_slug = 'designer';

    // Get all users with the Designer role
    $designers = get_users( array(
        'role' => $designer_role_slug,
        'fields' => 'ID'
    ) );

    // Add each designer as an administrator to the new site
    foreach ( $designers as $designer_id ) {
        add_user_to_blog( $blog_id, $designer_id, 'administrator' );
    }
}

Important: If you’re using a custom user group system (not default roles), adjust the get_users query to target your group instead of the role parameter.

Key Notes

  • MU plugins live in wp-content/mu-plugins/—create the directory if it doesn’t exist.
  • You don’t need to activate MU plugins; they load automatically for all sites in the network.
  • Always test this on a staging environment first to avoid breaking your live network.

内容的提问来源于stack exchange,提问作者Tian Reagon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:04:43