1

Is there a network equivalent to 'admin_init', or what do I have to do to add an action to network admin screens? I use 'admin_init' for an action on admin network screens, but the respective function is not executed. Thank you!

Edit: Here's what I want to do. The respective plugin writes custom database tables and should therefore only by activated on a per-site basis in a network.

function no_network_activation() {
    // echo 'test1 ';
    if ( is_network_admin() &&  isset($_GET['no-network-activation']) ) {
        // echo 'test2';
        add_action( 'network_admin_notices', 'no_network_activation_notice' );

        deactivate_plugins( plugin_basename( __FILE__ ) ); 

        if ( isset( $_GET['activate'] ) ) {
            unset( $_GET['activate'] );
        }
    }
}
add_action( 'admin_init', 'no_network_activation' );

function no_network_activation_notice(){
    // Echo an admin notice: no network activation possible
}

function myplugin_database_adder($networkwide) {
    // Redirect to network plugin url and add param to show the attempt of network-wide plugin activation
    if (is_multisite() && $networkwide) {
        header( 'Location: ' . network_admin_url( 'plugins.php?no-network-activation=true' ) );
        exit;
    }
    else {
        // Add the plugin database tables
        // [...] 
    }
}
register_activation_hook( __FILE__, 'myplugin_database_adder' );
6
  • What are you hooking into admin_init that's not working? Sep 9, 2021 at 11:06
  • Rereading the question few more times, I wonder if I misunderstood it the first time :-) Is the callback not running on network sites, or do you need to restrict your code to network pages? @joschi81
    – birgire
    Sep 9, 2021 at 11:51
  • @JacobPeattie: I will show this in an edit of my question.
    – joschi81
    Sep 9, 2021 at 15:08
  • @birgire: It seems to me that the code is not running on network admin pages (network plugin page). See the edit of my question to understand what I want to do.
    – joschi81
    Sep 9, 2021 at 15:09
  • 1
    try to start with nothing but echo in callback and build on top of that to make sure it runs on every page
    – birgire
    Sep 10, 2021 at 7:35

1 Answer 1

1

You could restrict your admin_init callback with e.g. a is_network_admin() check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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