uo_course_timer_logout
Filters whether a user should be logged out after a course timer expires, allowing for custom logic.
add_filter( 'uo_course_timer_logout', $callback, 10, 1 );
Description
Fires to determine if the user should be logged out after a course timer expires. Developers can return `false` to prevent the logout. This filter is crucial for controlling automatic user logout behavior triggered by course timers, ensuring a seamless experience.
Usage
add_filter( 'uo_course_timer_logout', 'your_function_name', 10, 1 );
Parameters
-
$course_id(mixed) - This parameter indicates whether the user should be logged out.
Return Value
The filtered value.
Examples
/**
* Prevent automatic logout for users with specific capabilities.
*
* This filter allows administrators to prevent users with certain capabilities
* from being automatically logged out by the course timer.
*
* @param bool $should_logout Whether to proceed with the logout.
* @param int $course_id The ID of the course.
* @return bool Returns false if the user has the capability to bypass logout, otherwise returns the original $should_logout value.
*/
add_filter( 'uo_course_timer_logout', function( $should_logout, $course_id ) {
// If the user can manage options, do not log them out.
if ( current_user_can( 'manage_options' ) ) {
return false;
}
// You could add more complex logic here, for example:
// if ( $course_id === 123 && current_user_can( 'editor' ) ) {
// return false; // Don't log out editors from course ID 123.
// }
// Otherwise, respect the original decision.
return $should_logout;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/course-timer.php:2128
public static function maybe_logout_user() {
check_ajax_referer( 'uo_course_timer_logout_nonce', 'nonce' );
$course_id = isset($_POST['courseID']) ? absint($_POST['courseID']) : 0;
$do_logout = apply_filters( 'uo_course_timer_logout', true, $course_id );
if( true === $do_logout && is_user_logged_in() && ! current_user_can( apply_filters('uo_time_enable_logout_mode_capability', 'manage_options', $course_id) ) ) {
remove_all_actions( 'wp_logout' );
wp_logout();
}
echo "1";
exit;
}