Title: wp_user_settings
Published: April 25, 2014
Last modified: April 28, 2025

---

# wp_user_settings()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#wp--skip-link--target)

Saves and restores user interface settings stored in a cookie.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#description)󠁿

Checks if the current user-settings cookie is updated and stores it. When no cookie
exists (different browser used), adds the last saved cookie restoring the settings.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#source)󠁿

    ```php
    function wp_user_settings() {

    	if ( ! is_admin() || wp_doing_ajax() ) {
    		return;
    	}

    	$user_id = get_current_user_id();
    	if ( ! $user_id ) {
    		return;
    	}

    	if ( ! is_user_member_of_blog() ) {
    		return;
    	}

    	$settings = (string) get_user_option( 'user-settings', $user_id );

    	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
    		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );

    		// No change or both empty.
    		if ( $cookie === $settings ) {
    			return;
    		}

    		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
    		$current    = 0;

    		if ( isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ) {
    			$current = (int) preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] );
    		}

    		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is.
    		if ( $current > $last_saved ) {
    			update_user_option( $user_id, 'user-settings', $cookie, false );
    			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
    			return;
    		}
    	}

    	// The cookie is not set in the current browser or the saved value is newer.
    	$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
    	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
    	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, '', $secure );
    	$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/option.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/option.php#L1698)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/option.php#L1698-L1743)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_doing_ajax()](https://developer.wordpress.org/reference/functions/wp_doing_ajax/)`wp-includes/load.php` |

Determines whether the current request is a WordPress Ajax request.

  | 
| [is_user_member_of_blog()](https://developer.wordpress.org/reference/functions/is_user_member_of_blog/)`wp-includes/user.php` |

Finds out whether a user is a member of a given blog.

  | 
| [update_user_option()](https://developer.wordpress.org/reference/functions/update_user_option/)`wp-includes/user.php` |

Updates user option with global blog capability.

  | 
| [get_user_option()](https://developer.wordpress.org/reference/functions/get_user_option/)`wp-includes/user.php` |

Retrieves user option that can be either per Site or per Network.

  | 
| [is_admin()](https://developer.wordpress.org/reference/functions/is_admin/)`wp-includes/load.php` |

Determines whether the current request is for an administrative interface page.

  | 
| [admin_url()](https://developer.wordpress.org/reference/functions/admin_url/)`wp-includes/link-template.php` |

Retrieves the URL to the admin area for the current site.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_user_settings/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_user_settings%2F)
before being able to contribute a note or feedback.