Function and usage of is_main_network() in wordpress


Answers ( 1 )


    The is_main_network() function in WordPress is used to determine if a specific network is the main network in a WordPress Multisite installation. Multisite is a feature in WordPress that allows for the creation and management of a network of sites within a single WordPress installation.

    Here's a detailed explanation of the is_main_network() function:

    Function Signature

    is_main_network( int $network_id = null ): bool


    • Determines if a Network is the Main Network: This function is used to check whether the given network ID corresponds to the main network of the WordPress Multisite installation.


    • $network_id (int, optional): The ID of the network to test.
      • Default Value: null. If no value is provided, the function checks if the current network (the network where the function is called) is the main network.

    Return Value

    • bool (Boolean):
      • Returns true if the specified network ID ($network_id) is the main network.
      • Also returns true if WordPress is not running in Multisite mode (since in that case, there is only one "main" network by default).
      • Returns false if the specified network is not the main network in a Multisite installation.


    Sample Usage 1: Check if Current Network is Main Network

    if ( is_main_network() ) {
        echo 'This is the main network.';
    } else {
        echo 'This is not the main network.';
    • This code checks if the current network is the main network. It does so by not passing any parameter to is_main_network(), which causes it to default to checking the current network.

    Sample Usage 2: Check if a Specific Network is Main Network

    $network_id = 2; // Example network ID
    if ( is_main_network( $network_id ) ) {
        echo 'Network ' . $network_id . ' is the main network.';
    } else {
        echo 'Network ' . $network_id . ' is not the main network.';
    • This code checks if the network with ID 2 is the main network. The $network_id variable is used to specify which network ID to check.

    Context of Use

    • This function is particularly useful in plugins or themes that need to behave differently on the main network compared to other networks in a Multisite setup.
    • It is also useful for administrative purposes, such as network-wide settings or features that should only be available on the main network.

    Remember that this function is specific to WordPress Multisite installations and will return true in a regular (non-Multisite) WordPress installation.

Leave an answer