• Resolved pits24

    (@pits24)


    Hi,
    I am looking for a plugin that deactivates (not delete) a certain role 30 days after it registers. Note that I currently have a separate registration page for this type of user.
    Any help is appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You could write your own plugin, or you could just write a php script.
    I think it would be best if you create a cron that runs daily, or twice a day.
    WordPress already has the method to remove the role :
    https://developer.ww.wp.xz.cn/reference/classes/wp_user/remove_role/

    Basically, the script would :
    – loop through the users
    – get all users that are registered today minus 30 days
    – remove the role from those users

    $users = get_users( fields' => array( 'ID', 'user_registered' ) );
     foreach($users as $user){
      $time = $user->user_registered;
       if(strtotime($time) < strtotime('-30 days')) {
         $u->remove_role( 'whatever-role' );
       }
     }
    Thread Starter pits24

    (@pits24)

    Thanks for the reply. I am trying to do this but when i run the cron function to test all the users in the role gets suspended regardless of registration time. Can you see what mistake i am making in the logic.

            
    $args1 = array(
                'role' => 'um_sub-admin',
                'date_query' => array(
                    'after' => date('Y-m-d', strtotime('-30 days'))
                ) 
            );
            
     $sub = get_users($args1);
    
    foreach ($sub as $user) {
        $udata = get_userdata( $user->ID );
        $registered = $udata->user_registered;
        $reg_date = date( "Y m d", strtotime( $registered ) );
        if( $reg_date < strtotime('-30 days') ) {
            
            $udata->remove_role( 'um_sub-admin' );
            $udata->add_role( 'suspended' );
        }
    
    }
    

    Are you saying this condition is always met ?
    if( $reg_date < strtotime(‘-30 days’) ) {

    Can you print this to the console and include it here ?
    $reg_date = date( “Y m d”, strtotime( $registered ) );

    Thread Starter pits24

    (@pits24)

    It worked out! I got the users with exact 30 days passed with date_query.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘User Role Plugin’ is closed to new replies.