Using WordPress ‘delete_network_option()’ PHP function

The delete_network_option() WordPress PHP function is used to remove a network option by its name. This function is similar to delete_option() but is specific to WordPress multisite networks.

Usage

Here’s a basic example of how to use this function:

delete_network_option( 1, 'my_network_option' );

In this example, 1 is the ID of the network and 'my_network_option' is the name of the option we want to delete.

Parameters

  • $network_id (int): This is the ID of the network. It can be null, in which case the function defaults to the current network ID.
  • $option (string): This is the name of the option to delete. It is expected to be a string and not SQL-escaped.

More information

See WordPress Developer Resources: delete_network_option()

This function is specifically designed for WordPress multisite networks. It’s related to the delete_option() function, which works for individual sites within a network.

Examples

Deleting a network option

In this example, we’re deleting an option named ‘theme_settings’ from network ID 2.

delete_network_option( 2, 'theme_settings' );

Deleting from the current network

If we don’t specify a network ID, the function will default to the current network.

delete_network_option( null, 'page_settings' );

Checking if an option deletion was successful

You can check if an option was successfully deleted by checking the function’s return value.

if ( delete_network_option( 3, 'plugin_settings' ) ) {
    echo "Option deleted successfully";
} else {
    echo "Option deletion failed";
}

Deleting multiple options

If you need to delete multiple options, you can call the function multiple times.

delete_network_option( 4, 'header_settings' );
delete_network_option( 4, 'footer_settings' );

Deleting an option and resetting to default

In this example, we delete an option and then recreate it with a default value.

delete_network_option( 5, 'color_scheme' );
add_network_option( 5, 'color_scheme', 'default' );

In this case, we use the add_network_option() function to add the ‘color_scheme’ option back with a default value after it has been deleted.