Created
September 27, 2024 18:33
-
-
Save bhwebworks/6bd15e00767d4d80fa4c4625aa950d82 to your computer and use it in GitHub Desktop.
Removes users (subscribers) on a WordPress site who haven't yet enrolled in a LearnDash course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'wp_loaded', 'bhww_remove_subscriber_users_not_enrolled' ); | |
/** | |
* Remove users who are only subscribers and who are not yet enrolled in the course. | |
* Only runs when a WordPress admin page loads. | |
*/ | |
function bhww_remove_subscriber_users_not_enrolled() { | |
// If this is not the WP admin, return early | |
if ( ! is_admin() ) | |
return; | |
$args = array( | |
'role__not_in' => array( 'administrator', 'editor', 'author', 'group_leader' ), | |
'orderby' => 'ID', | |
'order' => 'desc', | |
'number' => 100, // change this number as needed | |
); | |
// Array of WP_User objects | |
$subscribers = get_users( $args ); | |
$current_time = time(); | |
foreach ( $subscribers as $user ) { | |
$user_id = $user->ID; | |
$user_name = $user->user_login; | |
$course_id = 580; // LearnDash course ID - change as needed | |
$enrolled_in = learndash_user_get_enrolled_courses( $user_id ); | |
$quiz_progress = get_user_meta( $user_id, '_sfwd-quizzes', true ); | |
$get_mysql_time = current_time( 'mysql' ); | |
$registered_date_time = $user->user_registered; | |
$timestamp = strtotime( $registered_date_time ); | |
// Registered for at least 15 minutes? | |
$timestamp_plus_fifteen_minutes = $timestamp + 900; | |
if ( empty( $enrolled_in ) && empty( $quiz_progress ) ) { | |
// Registered more than fifteen minutes ago and haven't paid yet? Delete the user. | |
if ( $timestamp_plus_fifteen_minutes < $current_time ) { | |
// https://developer.wordpress.org/reference/functions/wp_delete_user/ | |
require_once( ABSPATH . 'wp-admin/includes/user.php' ); // Need this for wp_delete_user() | |
wp_delete_user( $user_id ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment