Using WordPress ‘delete_all_user_settings()’ PHP function

The delete_all_user_settings() WordPress PHP function removes all settings of the currently logged-in user.

Usage

You can use this function directly in your WordPress theme or plugin, for instance when you want to delete all user settings when they log out.

function reset_user_on_logout() {
    if (!is_user_logged_in()) {
        delete_all_user_settings();
    }
}
add_action('wp_logout', 'reset_user_on_logout');

In this example, the function reset_user_on_logout gets triggered when the user logs out, and if the user is no longer logged in, it deletes all their settings.

Parameters

  • This function does not accept any parameters.

More Information

See WordPress Developer Resources: delete_all_user_settings()

This function is not depreciated and is available in all recent versions of WordPress.

Examples

Clear User Settings on Deactivation of a Plugin

If you want to clear the user’s settings when they deactivate a certain plugin, you can do something like this:

function on_deactivation() {
    if (current_user_can('activate_plugins')) {
        delete_all_user_settings();
    }
}
register_deactivation_hook(__FILE__, 'on_deactivation');

When the plugin gets deactivated, all the user’s settings are cleared.

Clear User Settings on a Certain Page

If you want to delete all user settings when they visit a certain page, you can use this function:

function clear_user_settings_on_page() {
    if (is_page('clear-settings')) {
        delete_all_user_settings();
    }
}
add_action('template_redirect', 'clear_user_settings_on_page');

This function gets triggered when the user visits the ‘clear-settings’ page, deleting all their settings.

Clear User Settings After a Period of Inactivity

If you want to delete user settings after a certain period of inactivity, you can use the delete_all_user_settings() function in conjunction with a cron job:

function clear_inactive_user_settings() {
    if (get_transient('user_last_activity') < strtotime('-30 days')) {
        delete_all_user_settings();
    }
}
add_action('wp', 'clear_inactive_user_settings');

This function deletes the user’s settings if they have been inactive for more than 30 days.

Clear User Settings on Failed Login

You can clear the user’s settings if they fail to login:

function clear_settings_on_failed_login($username) {
    if (!username_exists($username)) {
        delete_all_user_settings();
    }
}
add_action('wp_login_failed', 'clear_settings_on_failed_login');

The function gets triggered when a login attempt fails, deleting the user’s settings if the username doesn’t exist.

Clear User Settings on Account Deletion

If you want to delete all settings of a user when their account is deleted, you can use the delete_all_user_settings() function like this:

function clear_settings_on_account_deletion($id, $reassign) {
    delete_all_user_settings();
}
add_action('delete_user', 'clear_settings_on_account_deletion', 10, 2);

This function gets triggered when a user’s account is deleted, removing all their settings.