Wordpress - Prevent network activation of plugin
A plugin header Network: false
will be ignored by WordPress … unfortunately. But the activation hook gets a parameter $network_wide
, and we can use that to deactivate the plugin during activation:
<?php
/**
* Plugin Name: Prevent Network Activation
* Plugin URI: http://wordpress.stackexchange.com/questions/76145/prevent-network-activation-of-plugin
* Network: false
*
* Note the 'Network' option will be ignored by WordPress.
*/
register_activation_hook( __FILE__, 'pna_check_network_activation' );
function pna_check_network_activation( $network_wide )
{
if ( ! $network_wide )
return;
deactivate_plugins( plugin_basename( __FILE__ ), TRUE, TRUE );
header( 'Location: ' . network_admin_url( 'plugins.php?deactivate=true' ) );
exit;
}
The answers here are overthought and too complex. Why deactivating the plugin instead of preventing activation? Something as simple as calling die('your error message here) upon activation will do the job.
function activate($networkwide) {
if (is_multisite() && $networkwide)
die('This plugin can\'t be activated networkwide');
}
register_activation_hook('your-plugin/index.php','activate');
Then when you try to activate in the panel, you will get a nice error on top of the page, with your error message.
You could simply hide it from the network-plugins list.
add_filter( 'all_plugins', 'wpse_76145_hide_network_plugin' );
function wpse_76145_hide_network_plugin( $all )
{
global $current_screen;
if( $current_screen->is_network )
unset($all['akismet/akismet.php']);
return $all;
}
And display a one-time network admin notice. Adapting the Q&A add_role() run only once?.
add_action( 'network_admin_notices', 'wpse_76145_admin_notice' );
function wpse_76145_admin_notice()
{
global $current_screen;
if( 'plugins-network' == $current_screen->id )
{
if ( wpse_25643_run_once( 'hide_akismet_network' ) )
echo '<div class="error">Akismet not available in Network mode</div>';
}
}
function wpse_25643_run_once( $key )
{
$test_case = get_option( 'run_once' );
if ( isset( $test_case[$key] ) && $test_case[$key] )
{
return false;
}
else
{
$test_case[$key] = true;
update_option( 'run_once',$test_case );
return true;
}
}
Or use this other technique: Add a notice to users upon first login to the admin area