Register Settings on WordPress Multisite

The use of WordPress for several blogs in the network can be useful to simplify several steps and is becoming increasingly popular. Whether you want the classical scenarios of blog hosting service or like to create multilingual websites or other ideas. Therefore, it is also important for plugin developers to use the functions and to expand or develop their own Plugins for it specifically.

Much is the same, but not all, and in this small article I would like to briefly explain how you set settings in the database when you activate a Plugin.

The best case for this is a Function of WordPress, which is triggered when activating a plugin register_activation_hook(). This Function will be called in init or constructor of the Plugin. In the called Function are the functions to store the settings of WordPress in the table options - add_option(). In Multisite there is also a function for it - add_site_option().

Now you have to separate, if the Plugin is activated within the Network, in the management of Multisite installation, or if it is only used in one of the blogs in the network or a single installation. There are currently no functions, but a value that is passed. The following example illustrates it:

register_activation_hook( __FILE__, 'fb_add_config' );
function fb_add_config() {

	$data = array( 
		'active' => 0, 
		'radio'  => 0,
		'link'   => 1, 
		'theme'  => 1, 
		'role'   => 'administrator', 
		'unit'   => 1, 
	);
	// if is active in network of multisite
	if ( is_multisite() && isset($_GET['networkwide']) && 1 == $_GET['networkwide'] ) {
		add_site_option( 'my_settings_id', $data );
	} else {
		add_option( 'my_settings_id', $data );
	}
}

The query of the value in the global GET can be integrated in the long time ago mentioned solution for Multisite and settings.

To solve other queries in a Multisite environment and to remove the setup or integrate menus in the admin area, the function is_plugin_active_for_network() is useful.

if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
	$values = get_site_option( 'my_settings_id' );
else
	$values = get_option( 'my_settings_id' );

Comments are closed.

2 comments

  1. Scott Elkin

    Would you consider re-writing this? It is really tough to read with all your comma's and I just don't follow it.

One pingback

  1. Adventskalender am 21. Dezember « F-LOG-GE