Add additional Network constants to wp-config.php dynamically

When starting with a fresh Network install, the first step is a single site install that has one additional line in the wp-config.php file:

define( 'WP_ALLOW_MULTISITE', true );

After that we have to go to Tools > Network Setup, enter some details like network name and super admin email and finally hit the button to run the setup.

Read More

The next step the pops up is actually two steps:

  1. Add additional constants to your wp-config.php file

    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    define( 'DOMAIN_CURRENT_SITE', 'example.com' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', true );
    define( 'BLOG_ID_CURRENT_SITE', true );
    
  2. Add some rules to your .htaccess file.

As I don’t do single site installs (no reason for that anymore), I’d like to add those constants dynamically to my wp-config.php file:

define( 'WP_ALLOW_MULTISITE', true );
if ( SOME_CHECK_IF_STEP-1_WAS_PASSED )
{
    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    define( 'DOMAIN_CURRENT_SITE', 'example.com' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', true );
    define( 'BLOG_ID_CURRENT_SITE', true );
}

So I don’t have to go to the wp-config.php each time and edit it again.

The 1st thing that came into my mind was to check the DB for the {$wpdb->prefix}sitemeta table, but I don’t want to do an additional query on each site load. The 2nd thing I thought of was to check if the blogs.dir already exists, but that’s not the case.

Question: How can I indicate that I already passed the first step of the network setup in way that doesn’t has a massive overhead or decreases performance.

Related posts

3 comments

  1. I haven’t tested this, but if you would need to find such a solution, I would probably try to do it in the following way by adding a script into the if ( SOME_CHECK_IF_STEP-1_WAS_PASSED ) condition you’ve described above, that would:

    • check the DB for the {$wpdb->prefix}sitemeta table;
    • if it does not exist -> return false;
    • if it does exists -> add a WP cron job to rewrite wp-config.php -> return true.

    The WP cron job would rewrite the wp-config.php to eliminate the check completely.

    This way, you only have an additional DB request until the network is set up and the cron job did its magic.

    Note sure this is worth the effort, though… 😉

  2. Inspect wp-admin/network.php and wp-admin/includes/schema.php files. You can create a condition based on WP_INSTALLING_NETWORK

    Update:

    if (is_admin()){
        /* First Step */
        if (!defined( 'WP_ALLOW_MULTISITE')){
                $current_wp_config = file_get_contents(ABSPATH . 'wp-config.php');
    
                $my_defines = "/* First we put WP_ALLOW_MULTISITE  */rn" .
                        "define('WP_ALLOW_MULTISITE', true); rn";
    
                $current_wp_config = str_replace("/* That's all, stop editing", $my_defines . "/* That's all, stop editing", $current_wp_config);       
                file_put_contents(ABSPATH . 'wp-config.php', $current_wp_config);   
        }   
        /* Second Step */
        if (!defined("WP_DEFINES_IMPORTED") && defined("WP_INSTALLING_NETWORK") && $_POST){
            $current_wp_config = file_get_contents(ABSPATH . 'wp-config.php');
    
            $my_defines = "/* Now we define */rn" .
                "define('WP_DEFINES_IMPORTED', true); rn" .
                "define('MULTISITE', true); rn".
                "define('SUBDOMAIN_INSTALL', true); rn".
                "define('DOMAIN_CURRENT_SITE', '". get_clean_basedomain() . "'); rn".
                "define('PATH_CURRENT_SITE', '". parse_url(  trailingslashit( get_option( 'home' ) ), PHP_URL_PATH ) . "'); rn".
                "define('SITE_ID_CURRENT_SITE', 1); rn".
                "define('BLOG_ID_CURRENT_SITE', 1); rn";
    
            $current_wp_config = str_replace("/* That's all, stop editing", $my_defines . "/* That's all, stop editing", $current_wp_config);       
            file_put_contents(ABSPATH . 'wp-config.php', $current_wp_config);   
        }
    }
    
    1. put this to mu-plugins folder.
    2. fresh wp install
    3. go to admin panel
    4. click to tools
    5. click to network setup
    6. run the setup
    7. ???
    8. profit!!!

    enter image description here

  3. If your setup is the same every time (subdomain install), then you may be able to paste the entire chunk in your first step:

    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    define( 'DOMAIN_CURRENT_SITE', $_SERVER[ 'HTTP_HOST' ] );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', true );
    define( 'BLOG_ID_CURRENT_SITE', true );

Comments are closed.