admin管理员组

文章数量:1287970

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' );

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' );
Share Improve this question edited Sep 13, 2021 at 4:47 winnewoerp asked Sep 9, 2021 at 10:18 winnewoerpwinnewoerp 1715 bronze badges 6
  • What are you hooking into admin_init that's not working? – Jacob Peattie Commented 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 Commented Sep 9, 2021 at 11:51
  • @JacobPeattie: I will show this in an edit of my question. – winnewoerp Commented 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. – winnewoerp Commented 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 Commented Sep 10, 2021 at 7:35
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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

本文标签: actionsIs there a filter called 39networkadmininit39