sunrise.php on VIP Go

VIP Go platform specific

This document is for sites running on VIP Go.

Learn more

Overview #

When developing for a WordPress multisite, it is often necessary to load some custom code very early in the WordPress application’s loading sequence, e.g. to implement some custom domain mapping functionality. For multisite instances on VIP Go, we make it easy to load custom code as part of WordPress Core’s sunrise.php.

Before discussing how, it’s important to note that sunrise.php loads very early in WordPress, and most of its functions won’t be available. Further, sunrise.php loads before mu-plugins, any active plugins, and the active theme. As such, what can be done in sunrise.php is limited, and largely confined to using pure PHP to set constants that override WordPress Core behaviour.

That said, to load additional code in sunrise.php, please follow these steps:

  1. Create a vip-config/client-sunrise.php file in your site’s repository. On VIP Go, if the file is present, it will be loaded automatically (ref).

To achieve the same behaviour on a local development environment:

  1. Copy wp-content/mu-plugins/lib/sunrise/sunrise.php to wp-content/sunrise.php.
  2. Add define('SUNRISE', true) to wp-config.php.

↑ Top ↑

Guidelines #

As already mentioned, the purpose of vip-config/client-sunrise.php is to execute PHP early in the WordPress cycle, before most of WordPress, including any theme or plugin code, loads. Typical uses for this file therefore share a few characteristics:

  • The problem being solved requires early execution (for instance, for performance reasons or to override aspects of the request very early on.) Code that can run later in execution should typically be written in the plugin or theme context and leverage WordPress’ Plugin API.
  • The problem can be solved with (mostly) pure PHP.
  • The problem being solved does not require database access or access to any persistent data, but often makes decisions or performs actions based on intrinsic characteristics of the request, such as the domain or URI.  Because it executes on every single request handled by WordPress, make sure your use of sunrise.php does not introduce the kinds of performance penalties associated with queries or other expensive operations.
  • The scope of the problem being solved is well understood and minimized. Code executed within sunrise.php is kind of like a sledgehammer, and can have far-reaching effects. Do as much as you can to limit the conditions under which the code executes (such as limiting it to only certain URLs or certain domains) to reduce the probability of unintended side-effects.

↑ Top ↑

Example: multiple domains in multisite on Go #

By default, VIP Go enforces the use of a single domain per site in a multisite (notably, this is not exactly the same as core multisite, which will cause a domain not associated with any site in the network to resolve to site_id 1.) Some VIPs have found it useful to allow multiple domains to resolve to a single site in the network either temporarily or permanently. In the example below, this multi-domain configuration is limited to REST API requests, but it could be more general, or differently targeted.

// Allow REST API requests to be served on one of several domains
$clientslug_custom_sunrise_domains = ['', '', ''];
// Cause each of these domains to load site_id 1
$clientslug_custom_sunrise_site_id = 1;
// @codingStandardsIgnoreLine -- use of $_SERVER okay here
if ( in_array( $_SERVER['HTTP_HOST'], $clientslug_custom_sunrise_domains, true ) && 0 === strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-json' ) ) {
	// These domains are each associated with site_id 1 of network 1
	$current_blog = get_site( $clientslug_custom_sunrise_site_id );
	$current_site = get_network( 1 ); // This should always be 1, unless you are running multiple WordPress networks.

Other uses of client-sunrise.php might include logic around redirects (that don’t require database lookups) or other manipulation of fundamental WordPress constants or globals based on the request.

If you have questions about whether a solution involving sunrise is appropriate for your situation, please reach out to VIP support — we’d be happy to discuss your issue further.

Ready to get started?

Drop us a note.

No matter where you are in the planning process, we’re happy to help, and we’re actual humans here on the other side of the form. 👋 We’re here to discuss your challenges and plans, evaluate your existing resources or a potential partner, or even make some initial recommendations. And, of course, we’re here to help any time you’re in the market for some robust WordPress awesomeness.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.