1

I have a function called by a daily cron job that automatically changes the role of users after a period of time. The 1st role update should happen 120 days after registration, and a 2nd role update after 210 days. However, it didn't have any effect, and the user roles didn't change (it should have been updated for a few users who now reached 120 days).

Anyone noticing any problem with the code ? Thank you

if (!wp_next_scheduled('user_update_hook')) {
wp_schedule_event( time(), 'daily', 'user_update_hook' );
}

add_action ( 'user_update_hook', 'user_update_function' );
function user_update_function()
{
$blogusers = get_users();
foreach ( $blogusers as $user ) {
    if (in_array("administrator", $user->roles)){
    }else{
        $registration_date =(get_userdata($user->ID)->user_registered);
        $now = time(); // or your date as well
        $your_date = strtotime($registration_date);
        $datediff = $now - $your_date;
        $total_days = round($datediff / (60 * 60 * 24));
        if($total_days >= 120 && $total_days <= 209 ){
            $u = new WP_User( $user->ID );
            $u->set_role( 'optimizemember_level4' );
        }elseif($total_days >= 210){
            $u = new WP_User( $user->ID );
            $u->set_role( 'optimizemember_level5' );
        }else{
            return '';
        }
    }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.