0

I have set up Wordpress with a multisite network. I have been searching around for hours and can't seem to find much documentation on setting custom defaults for all the new sites that are created on the network.

I am using a theme with a child theme installed.

Firstly I would like the default theme for all new sites created on the network to be my child-theme that is installed and Every new site starts with a basic default static front page.

Could this work or should i add it as a function in mu-plugin:

/** Sets default theme for new sites on network */
define( 'WP_DEFAULT_THEME', 'theme-child');

Secondly, Every new site created I need to give access to all administrators on the network. Allowing them to work on all the sites automatically.

Should I try and create a mu-plugin if so please give some direction, or if there is a different way please advise. I haven't coded much in php. Please advise how this can be done.

Thanks

2 Answers 2

0

So I figured it out: I added the default theme in wp-config:

// Setting default theme for newly created sites 
define( 'WP_DEFAULT_THEME', 'theme-child' );

for the static front page I added:

# ON SIGNUP OF NEW SITES A NEW PAGE IS CREATED CALLED "Homepage" AND SET AS 
THE STATIC FRONT PAGE

add_action( 'wpmu_new_blog', 'process_extra_field_on_blog_signup', 10, 6 );

function process_extra_field_on_blog_signup( $blog_id, $user_id, $domain, 
$path, $site_id, $meta ) {
switch_to_blog($blog_id);


$new_page_title = 'Homepage'; //page title
$new_page_content = 'TEST'; //content of page goes here
$new_page_template = ''; //page template.

$page_check = get_page_by_title($new_page_title);
$new_page = array(
        'post_type' => 'page', 
        'post_title' => $new_page_title,
        'post_content' => $new_page_content,
        'post_status' => 'publish',
        'post_author' => 1,
);
if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
                update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
        }
}

$homepage = get_page_by_title( 'Homepage' );
if ( $homepage )
{
    update_blog_option( $blog_id, 'page_on_front', $homepage->ID );
    update_blog_option( $blog_id, 'show_on_front', 'page' );
}
restore_current_blog();
}

Now I just need to make all users administrators on newly created sites.

1
  • Did you add both of these to the wp-config.php file? Commented Apr 16, 2021 at 16:22
0

To achieve your goals in a WordPress multisite network, you can indeed use a combination of the wp-config.php file and a custom plugin mu-plugin. please follow a step-by-step guide below the scenario.

1. Set Default Theme for New Sites

Add the following line to your wp-config.php file

define('WP_DEFAULT_THEME', 'your-child-theme-folder-name');

Replace 'your-child-theme-folder-name' with the actual folder name of your child theme.

2. Set Default Static Front Page for New Sites

To set a default static front page for all new sites, you can use a mu-plugin. Create a new PHP file e.g. custom-defaults.php and place it in the wp-content/mu-plugins directory. If the mu-plugins directory doesn't exist, you can create it.

Inside custom-defaults.php, add the following code

<?php
/**
 * Set default static front page for new sites in a multisite network.
 */
function set_default_front_page() {
    update_site_option('site_front_page', 'your-static-front-page-id');
    update_site_option('site_posts_page', 'your-blog-posts-page-id');
}
add_action('wpmu_new_blog', 'set_default_front_page');

Replace 'your-static-front-page-id' and 'your-blog-posts-page-id' with the actual page IDs for your static front page and blog posts page.

3. Grant Access to All Administrators

For granting access to all administrators on the network, you can use a mu-plugin as well. Create another PHP file e.g. grant-admin-access.php in the wp-content/mu-plugins directory.

Inside grant-admin-access.php, add the following code

<?php
/**
 * Grant access to all administrators on the network.
 */
function grant_admin_access_to_all_sites($user_id, $password, $meta) {
    $user = new WP_User($user_id);
    $user->for_each_blog(function ($blog_id) use ($user_id) {
        grant_super_admin($user_id);
    });
}
add_action('wpmu_new_user', 'grant_admin_access_to_all_sites', 10, 3);

This code uses the grant_super_admin() function to give super admin privileges to a user on all sites in the network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.