Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active April 2, 2020 18:08
Show Gist options
  • Save MaximeCulea/f15d1268a3056c7a6a2c732ed043166c to your computer and use it in GitHub Desktop.
Save MaximeCulea/f15d1268a3056c7a6a2c732ed043166c to your computer and use it in GitHub Desktop.
Cheatset for multinetwork
<?php
/**
* Grant newly superadmin user to all networks when working in a multi-network context
* @author Maxime CULEA
*/
add_action( 'grant_super_admin', 'superadmin_is_superadmin_for_all_networks' );
function superadmin_is_superadmin_for_all_networks( $user_id ) {
remove_action( 'grant_super_admin', 'superadmin_is_superadmin_for_all_networks' );
global $wpdb;
$results = $wpdb->get_col( "SELECT id FROM {$wpdb->site}" );
if ( ! empty( $results ) ) {
$current_network = get_current_network_id();
foreach ( $results as $network_id ) {
switch_to_network( (int) $network_id );
grant_super_admin( $user_id );
}
switch_to_network( $current_network );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment