The attach_session_information WordPress PHP filter allows you to modify the information attached to a newly created session.
Usage
add_filter('attach_session_information', 'my_custom_function', 10, 2); function my_custom_function($session, $user_id) { // your custom code here return $session; }
Parameters
$session
(array): The array of extra data to be attached to the session.$user_id
(int): The user ID for which the session is being created.
More information
See WordPress Developer Resources: attach_session_information
Examples
Add user role to the session information
Attach the user’s role to the session information.
add_filter('attach_session_information', 'add_user_role_to_session', 10, 2); function add_user_role_to_session($session, $user_id) { $user = get_userdata($user_id); $roles = $user->roles; $session['user_role'] = $roles[0]; return $session; }
Add registration date to the session information
Attach the user’s registration date to the session information.
add_filter('attach_session_information', 'add_registration_date_to_session', 10, 2); function add_registration_date_to_session($session, $user_id) { $user = get_userdata($user_id); $registration_date = $user->user_registered; $session['registration_date'] = $registration_date; return $session; }
Add user display name to the session information
Attach the user’s display name to the session information.
add_filter('attach_session_information', 'add_display_name_to_session', 10, 2); function add_display_name_to_session($session, $user_id) { $user = get_userdata($user_id); $display_name = $user->display_name; $session['display_name'] = $display_name; return $session; }
Add user email to the session information
Attach the user’s email address to the session information.
add_filter('attach_session_information', 'add_user_email_to_session', 10, 2); function add_user_email_to_session($session, $user_id) { $user = get_userdata($user_id); $user_email = $user->user_email; $session['user_email'] = $user_email; return $session; }
Add custom user meta to the session information
Attach custom user meta data to the session information.
add_filter('attach_session_information', 'add_custom_user_meta_to_session', 10, 2); function add_custom_user_meta_to_session($session, $user_id) { $custom_meta = get_user_meta($user_id, 'custom_meta_key', true); $session['custom_meta'] = $custom_meta; return $session; }