10

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.

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.

  • 2
    You shouldnt try to automate everything: i.imgur.com/OoF9Md7.png – Ünsal Korkmaz Aug 12 '13 at 12:32
  • 1
    it's actually unbelievable the process is still the same while more and more WP installation become multisite – JMau Aug 12 '13 at 12:46
  • @ÜnsalKorkmaz If you've seen my config setup, you'll understand that this is only one more step forward. Next thing: Grunt wp-config generator. – kaiser Aug 12 '13 at 12:55
  • I can't understand why you would need to check if a condition exists for every page load and then define constants. You are with running a multisite set-up or you're not. If you're talking about automating WordPress set-up you can look into something like YeoPress github.com/wesleytodd/YeoPress – kingkool68 Aug 12 '13 at 13:21
  • @kingkool68 Easy. Because I don't want to uncomment/comment those constants each time I set up a network install. It's no overhead and the step simply is unnecessary. And no, I'm not talking about automating WP setup. Btw: Yeoman uses Grunt for tasks (which I mentioned above). – kaiser Aug 12 '13 at 21:38
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... ;)

  • BTW, the script could change the wp-config.php file directly, but I suppose that it would be locked at that point... – Alain Schlesser Mar 10 '16 at 12:17
  • That cron job idea is funny :) Thanks for that. Btw, you can edit your answer. – kaiser Mar 10 '16 at 12:43
-1

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  */\r\n" .
                    "define('WP_ALLOW_MULTISITE', true); \r\n";

            $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 */\r\n" .
            "define('WP_DEFINES_IMPORTED', true); \r\n" .
            "define('MULTISITE', true); \r\n".
            "define('SUBDOMAIN_INSTALL', true); \r\n".
            "define('DOMAIN_CURRENT_SITE', '". get_clean_basedomain() . "'); \r\n".
            "define('PATH_CURRENT_SITE', '". parse_url(  trailingslashit( get_option( 'home' ) ), PHP_URL_PATH ) . "'); \r\n".
            "define('SITE_ID_CURRENT_SITE', 1); \r\n".
            "define('BLOG_ID_CURRENT_SITE', 1); \r\n";

        $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

  • I already checked that. Point is that wp-load.php pulls in wp-config.php earlier, so WP_INSTALLING_NETWORK won't be defined. But even if it would be available, it's only available during network setup. What would I do to leave the constants in after the installation has run? – kaiser Aug 12 '13 at 13:48
  • No.. you should use a function to execute once and write your defines to wp-config.php file once. You can think it like register_activation_hook – Ünsal Korkmaz Aug 12 '13 at 20:47
  • index.php pulls ./wp-blog-header.php), which then pulls wp-load.php and from there wp-config.php is called. An overview can be found here. admin.php itself gets called before setup.php and network.php (and calls wp-load.php itself at the beginning), so config has been running already. – kaiser Aug 12 '13 at 21:35
  • And as WP_INSTALLING_NETWORK gets define() without checking if it is already defined(), I would create an Fatal Error there. Please proof me wrong and give it a test. :) – kaiser Aug 12 '13 at 21:36
  • 2
    No Ünsal, I'm not. I'm even ignoring the fact that I could take the off-topic comic strip above as trolling or just rude. I'm searching for a solution. Period. But having a writable wp-config.php file is a security risk. Consider that you got all important details in there. I showed you that there's no way that this would work (without having a writable config file). What I'm searching for is an indicator (which doesn't slow down the system) that I can check against and use as switch. Everything else won't work. – kaiser Aug 13 '13 at 19:16
-1

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 );

  • As you can see in the question, I am quite aware of the needed constants. What I am searching for is a reliable indicator if the setup already passed step 1, nothing else. Please see the highlighted actual question at bottom. Thanks for your efforts tough. – kaiser Jan 26 '16 at 21:30
  • My answer was to challenge whether you even need the indicator at all, but rather rely on the server variable to serve the correct DOMAIN_CURRENT_SITE constant that WordPress gives you after completing the internal setup. – Geoff Jan 27 '16 at 18:15

protected by Community Jan 23 '15 at 9:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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