wp_user_settings

Saves and restores user interface settings stored in a cookie.

Description

wp_user_settings(); 

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.


Usage

  1. if ( !function_exists( 'wp_user_settings' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_user_settings(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/option.php  
  1. function wp_user_settings() { 
  2.  
  3. if ( ! is_admin() || wp_doing_ajax() ) { 
  4. return; 
  5.  
  6. if ( ! $user_id = get_current_user_id() ) { 
  7. return; 
  8.  
  9. return; 
  10.  
  11. $settings = (string) get_user_option( 'user-settings', $user_id ); 
  12.  
  13. if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) { 
  14. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] ); 
  15.  
  16. // No change or both empty 
  17. if ( $cookie == $settings ) 
  18. return; 
  19.  
  20. $last_saved = (int) get_user_option( 'user-settings-time', $user_id ); 
  21. $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0; 
  22.  
  23. // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is 
  24. if ( $current > $last_saved ) { 
  25. update_user_option( $user_id, 'user-settings', $cookie, false ); 
  26. update_user_option( $user_id, 'user-settings-time', time() - 5, false ); 
  27. return; 
  28.  
  29. // The cookie is not set in the current browser or the saved value is newer. 
  30. $secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) ); 
  31. setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure ); 
  32. setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure ); 
  33. $_COOKIE['wp-settings-' . $user_id] = $settings;