Using WordPress ‘network_settings_add_js()’ PHP function

The network_settings_add_js WordPress PHP function prints JavaScript in the header on the Network Settings screen.

Usage

To use the network_settings_add_js function, simply call it within the admin_head action hook:

add_action('admin_head', 'network_settings_add_js');

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: network_settings_add_js

This function is primarily used for the Network Settings screen in a WordPress Multisite installation.

Examples

Add custom JavaScript to Network Settings screen

This example demonstrates how to add custom JavaScript to the Network Settings screen.

function my_custom_js() {
  // Check if the current screen is the Network Settings screen
  if ('settings_page_network_settings' === get_current_screen()->id) {
    // Add your custom JavaScript here
    echo "<script>console.log('Hello, Network Settings!');</script>";
  }
}

// Hook the custom function to admin_head
add_action('admin_head', 'my_custom_js');

Change the background color of Network Settings screen

This example shows how to change the background color of the Network Settings screen using JavaScript.

function change_bg_color() {
  if ('settings_page_network_settings' === get_current_screen()->id) {
    echo "<script>document.body.style.backgroundColor = '#f0f0f0';</script>";
  }
}

add_action('admin_head', 'change_bg_color');

Display a custom alert message on the Network Settings screen

This example demonstrates how to display a custom alert message when the Network Settings screen is loaded.

function display_alert_message() {
  if ('settings_page_network_settings' === get_current_screen()->id) {
    echo "<script>alert('Welcome to the Network Settings page!');</script>";
  }
}

add_action('admin_head', 'display_alert_message');

Hide a specific input field on the Network Settings screen

This example shows how to hide a specific input field on the Network Settings screen using JavaScript.

function hide_input_field() {
  if ('settings_page_network_settings' === get_current_screen()->id) {
    echo "<script>document.querySelector('#input_field_id').style.display = 'none';</script>";
  }
}

add_action('admin_head', 'hide_input_field');

Automatically focus on a specific input field on the Network Settings screen

This example demonstrates how to automatically set the focus on a specific input field when the Network Settings screen is loaded.

function focus_input_field() {
  if ('settings_page_network_settings' === get_current_screen()->id) {
    echo "<script>document.querySelector('#input_field_id').focus();</script>";
  }
}

add_action('admin_head', 'focus_input_field');