Using WordPress ‘delete_users_add_js()’ PHP function

The delete_users_add_js() WordPress PHP function is used to add JavaScript to delete users page in WordPress.

Usage

Here’s an example of how to use this function:

add_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );

In this example, add_action is used to attach delete_users_add_js function to admin_print_scripts-users.php hook. This means JavaScript will be added to the delete users page when it loads.

Parameters

  • This function does not accept any parameters.

More information

See WordPress Developer Resources: delete_users_add_js

This function was introduced in WordPress 3.1 and it’s located in wp-admin/includes/user.php.

Examples

Adding JavaScript to Delete Users Page

This example shows how to add JavaScript to the delete users page.

// Attach the function to the 'admin_print_scripts-users.php' hook.
add_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );

Using in a Plugin

If you’re creating a plugin and you want to add some JavaScript to the delete users page, you can use this function.

// Inside your plugin file
add_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );

Use in a Theme

You can also use this function in a theme to add JavaScript to the delete users page.

// Inside your theme's functions.php file
add_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );

Conditional Usage

This function can be used conditionally. For example, if you only want to add JavaScript to the delete users page for a specific user role.

// If current user is administrator
if ( current_user_can( 'administrator' ) ) {
    add_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );
}

Removing the Function

If for some reason you need to remove the JavaScript from the delete users page, you can use the remove_action function.

// Remove the JavaScript from the delete users page
remove_action( 'admin_print_scripts-users.php', 'delete_users_add_js' );

In this example, remove_action is used to detach delete_users_add_js function from the admin_print_scripts-users.php hook. This means JavaScript will no longer be added to the delete users page.