Documentation Using A Shared/Common Theme Across Multiple Sites

Using A Shared/Common Theme Across Multiple Sites

Overview #

Do you have multiple themes that are basically the same? Right now if you want to make a change to all of your sites, you likely have to commit the same code to each theme repository. This is time-consuming and requires more effort on both your and our part. By using a shared theme, changes made to one site can, at your discretion, be applied to all of your sites.

There are various techniques to implement a common theme.

↑ Top ↑

Code-level Configuration #

The trick is to replace any per-theme code with some code that is wrapped in conditional tags. Many of our VIPs create configuration arrays in their functions.php and then switch to the different configurations based on the value of home_url(). Here’s a quick example on how to approach this:

add_action( 'init', 'my_init_site', 1 ); // let's initialize our settings very early on

/**
 * Initilize settings based on the site url
 */
function my_init_site() {
	global $my_settings;

	$my_settings = array();

	switch ( parse_url( home_url(), PHP_URL_HOST ) ) {
		case 'cooltechreviewsite.com':
			$my_settings['twitter_username'] = 'cooltechreviewsite';
			break;
		case 'coolfoodiesite.com':
			$my_settings['twitter_username'] = 'coolfoodiesite';
			break;
	}
}

/**
 * Function gets a setting by name
 */
function my_get_site_setting( $name, $default_value = '' ) {
	global $my_settings;
	return isset( $my_settings[$name] ) ? $my_settings[$name] : $default_value;
}

function my_twitter_link() {
	$my_twitter_username = my_get_site_setting( 'twitter_username' );
	if( ! empty( $my_twitter_username ) )
		printf( '<a href="%s">Follow us on Twitter!</a>', esc_url( 'http://twitter.com/' . $my_twitter_username ) );
}

Other options include having a config folder with unique configurations/templates for each site:

function my_load_site_config() {
	$config_path = '';

	switch ( parse_url( home_url(), PHP_URL_HOST ) ) {
		case 'cooltechreviewsite.com':
		case 'dev.cooltechreviewsite.com':
			$config_path = __DIR__ . '/config/cooltechreviewsite.com/config.php';
			break;
		case 'coolfoodiesite.com':
		case 'qa.coolfoodiesite.com':
			$config_path = __DIR__ . '/config/coolfoodiesite.com/config.php';
			break;
	}

	if ( $config_path && file_exists( $config_path ) )
		require( $config_path );
	else
		trigger_error( 'Uh oh, config not found!' );
}

↑ Top ↑

Options/Customizer Panel #

A better, but more involved, solution is to use a settings page to control the per-theme settings. CheezCAP makes this easier. Going this route will allow you to change settings without having to commit code changes and is especially useful on a large network of sites.

You can also integrate this into the Theme Customizer to provide a real-time view of the options as they are updated.

↑ Top ↑

Parent-Child Theme #

In some cases, it may not be feasible to have the same theme work for all your sites (e.g. unique template requirements). If you do, the parent-child theme approach might make sense. The idea here is that you try to encapsulate as much of the functionality into the parent (sometimes called the “master”) and have child themes for individual sites to override things as necessary. Note that if you are planning for a large number of sites and themes, this is not a good solution.

↑ Top ↑

Shared Plugins #

If you can’t easily follow the common theme approach and are maintaining several themes, it might still sense to abstract shared functionality into plugins that be shared across these theme via a shared plugins repo. The idea here is that each theme will unique templates but will share the larger functional bits via plugins. Your team will get access to a separate SVN repo where the plugins can be stored.

If you structure your plugins in this repo correctly, you can load them using our helper function. If you add the plugin with a matching name and file shared-plugins/my-plugin/my-plugin.php, it can be loaded like follows:

wpcom_vip_load_plugin( 'my-plugin', 'shared-plugins' );

Some notes on shared plugins repos:

* plugins_url() will work in this repo
* If you need to reference the URL or path of a different VIP theme, you can use the wpcom_vip_theme_url or wpcom_vip_theme_dir functions.