Support » Plugin: Restrict User Access - Membership Plugin with Force » remove levels with php code

  • Resolved freedom667

    (@freedom667)


    hi there. I found a way with user expiration but I could not remove the level. how can I remove the all levels with php code?

    this is my code:

    $current_date = date("Y-m-d");
    $date_to_compare = date("Y-m-d",time()+86400); //1 day later
    
    if (strtotime($date_to_compare) > strtotime($current_date)) {
    		   		foreach ($user_levels as $level => $value) {
                		$user->remove_level($level);
                		echo "too late ";
            		}
    			}
            }

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter freedom667

    (@freedom667)

    hey, why don’t you reply me?

    Plugin Author Joachim Jensen

    (@intoxstudio)

    You can use the API like this:

    
    rua_get_user($user_id)->remove_level($level_id)
    

    In your snippet, the levels will always be removed since $date_to_compare will always be higher than $current_date. I hope this helps!

    RUA is developed in my spare time, and I try my best to answer support questions in a timely manner. Unfortunately I cannot help with specific customizations, but I can give general answers about API usage.

    Thread Starter freedom667

    (@freedom667)

    I wrote it in foreach loop but does not work

    Plugin Author Joachim Jensen

    (@intoxstudio)

    Can you show me a bigger snippet of what your code looks like? Especially what is $user_levels and what is $user?

    Thread Starter freedom667

    (@freedom667)

    I made it this is my function. If you want, you can add to your plugin but this function have to work with cron.

    function my_cronjob_action () {
         $current_date = date("Y-m-d");
         $users = get_users();
         foreach( $users as $user){
            $date_to_compare = get_the_author_meta( 'year_of_birth', $user->ID );
            if (strtotime($current_date) > strtotime($date_to_compare) ) {
                $user_levels = rua_get_user($user)->get_level_ids(false, false, true);
                foreach ($user_levels as $level) {
                    rua_get_user($user)->remove_level($level);
    
                }
            }
            echo $date_to_compare;
        }
    
    } add_action('my_cronjob_action', 'my_cronjob_action');

    you will give a date to a user, then the date which you give to user bigger than today, the levels will remove from user

    • This reply was modified 1 year, 1 month ago by freedom667.
    • This reply was modified 1 year, 1 month ago by freedom667.
    • This reply was modified 1 year, 1 month ago by freedom667.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘remove levels with php code’ is closed to new replies.