wp_session_cleanup

Clean up expired sessions by removing data and their expiration entries from the WordPress options table.

Description

wp_session_cleanup(); 

This method should never be called directly and should instead be triggered as part of a scheduled task or cron job.


Usage

  1. if ( !function_exists( 'wp_session_cleanup' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/libraries/wp-session.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_session_cleanup(); 
  7.  

Defined (2)

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

/deprecated/includes/libraries/wp-session.php  
  1. function wp_session_cleanup() { 
  2. global $wpdb; 
  3.  
  4. if ( defined( 'WP_SETUP_CONFIG' ) ) { 
  5. return; 
  6.  
  7. if ( ! defined( 'WP_INSTALLING' ) ) { 
  8. $expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" ); 
  9.  
  10. $now = current_time( 'timestamp' ); 
  11. $expired_sessions = array(); 
  12.  
  13. foreach( $expiration_keys as $expiration ) { 
  14. // If the session has expired 
  15. if ( $now > intval( $expiration->option_value ) ) { 
  16. // Get the session ID by parsing the option_name 
  17. $session_id = substr( $expiration->option_name, 20 ); 
  18.  
  19. if( (int) -1 === (int) $session_id || ! preg_match( '/^[a-f0-9]{32}$/', $session_id ) ) { 
  20. continue; 
  21.  
  22. $expired_sessions[] = $expiration->option_name; 
  23. $expired_sessions[] = esc_sql( "_wp_session_$session_id" ); 
  24.  
  25. // Delete all expired sessions in a single query 
  26. if ( ! empty( $expired_sessions ) ) { 
  27. $option_names = implode( "', '", $expired_sessions ); 
  28. $wpdb->query( $wpdb->prepare( 
  29. DELETE FROM $wpdb->options 
  30. WHERE option_name 
  31. IN ('%s') 
  32. ",  
  33. $option_names 
  34. ) ); 
  35.  
  36. // Allow other plugins to hook in to the garbage collection process. 
  37. do_action( 'wp_session_cleanup' ); 
/includes/Libraries/Session/wp-session.php  
  1. function wp_session_cleanup() { 
  2. global $wpdb; 
  3.  
  4. if ( defined( 'WP_SETUP_CONFIG' ) ) { 
  5. return; 
  6.  
  7. if ( ! defined( 'WP_INSTALLING' ) ) { 
  8. $expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" ); 
  9.  
  10. $now = current_time( 'timestamp' ); 
  11. $expired_sessions = array(); 
  12.  
  13. foreach( $expiration_keys as $expiration ) { 
  14. // If the session has expired 
  15. if ( $now > intval( $expiration->option_value ) ) { 
  16. // Get the session ID by parsing the option_name 
  17. $session_id = substr( $expiration->option_name, 20 ); 
  18.  
  19. if( (int) -1 === (int) $session_id || ! preg_match( '/^[a-f0-9]{32}$/', $session_id ) ) { 
  20. continue; 
  21.  
  22. $expired_sessions[] = $expiration->option_name; 
  23. $expired_sessions[] = esc_sql( "_wp_session_$session_id" ); 
  24.  
  25. // Delete all expired sessions in a single query 
  26. if ( ! empty( $expired_sessions ) ) { 
  27. $option_names = implode( "', '", $expired_sessions ); 
  28. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" ); 
  29.  
  30. // Allow other plugins to hook in to the garbage collection process. 
  31. do_action( 'wp_session_cleanup' );