The get_main_network_id WordPress PHP filter allows you to modify the main network ID in a multisite setup.
Usage
add_filter('get_main_network_id', 'my_custom_main_network_id');
function my_custom_main_network_id($main_network_id) {
// Your custom code here
return $main_network_id;
}
Parameters
$main_network_id(int): The ID of the main network.
More information
See WordPress Developer Resources: get_main_network_id
Examples
Change the main network ID
Modify the main network ID to a custom value.
add_filter('get_main_network_id', 'change_main_network_id');
function change_main_network_id($main_network_id) {
// Set a custom main network ID
$new_main_network_id = 2;
return $new_main_network_id;
}
Use the main network ID to redirect users
Redirect users to a custom URL based on the main network ID.
add_filter('get_main_network_id', 'redirect_users_based_on_main_network_id');
function redirect_users_based_on_main_network_id($main_network_id) {
// Check if the current network ID is the main network
if (get_current_blog_id() == $main_network_id) {
// Redirect users to a custom URL
wp_redirect('https://www.example.com/custom-url/');
exit;
}
return $main_network_id;
}
Modify the main network ID conditionally
Change the main network ID based on the current user’s role.
add_filter('get_main_network_id', 'conditionally_modify_main_network_id');
function conditionally_modify_main_network_id($main_network_id) {
// Get the current user
$current_user = wp_get_current_user();
// Check if the current user has the "editor" role
if (in_array('editor', $current_user->roles)) {
// Set a custom main network ID for editors
$main_network_id = 3;
}
return $main_network_id;
}
Log the main network ID
Log the main network ID to a custom log file.
add_filter('get_main_network_id', 'log_main_network_id');
function log_main_network_id($main_network_id) {
// Log the main network ID to a custom log file
error_log('Main network ID: ' . $main_network_id, 3, '/path/to/your/custom-log-file.log');
return $main_network_id;
}
Modify the main network ID using a custom function
Use a custom function to calculate a new main network ID.
add_filter('get_main_network_id', 'modify_main_network_id_using_custom_function');
function modify_main_network_id_using_custom_function($main_network_id) {
// Custom function to calculate a new main network ID
function calculate_new_main_network_id($old_id) {
// Perform some calculations and return a new ID
return $old_id + 1;
}
// Set the main network ID using the custom function
$main_network_id = calculate_new_main_network_id($main_network_id);
return $main_network_id;
}