Title: remove levels with php code
Last modified: July 23, 2020

---

# remove levels with php code

 *  Resolved [freedom667](https://wordpress.org/support/users/freedom667/)
 * (@freedom667)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fremove-levels-with-php-code%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Thread Starter [freedom667](https://wordpress.org/support/users/freedom667/)
 * (@freedom667)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13230265)
 * hey, why don’t you reply me?
 *  Plugin Author [Joachim Jensen](https://wordpress.org/support/users/intoxstudio/)
 * (@intoxstudio)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13259289)
 * 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](https://wordpress.org/support/users/freedom667/)
 * (@freedom667)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13266675)
 * I wrote it in foreach loop but does not work
 *  Plugin Author [Joachim Jensen](https://wordpress.org/support/users/intoxstudio/)
 * (@intoxstudio)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13295889)
 * 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](https://wordpress.org/support/users/freedom667/)
 * (@freedom667)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13305028)
 * 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 5 years, 9 months ago by [freedom667](https://wordpress.org/support/users/freedom667/).
    -  This reply was modified 5 years, 9 months ago by [freedom667](https://wordpress.org/support/users/freedom667/).
    -  This reply was modified 5 years, 9 months ago by [freedom667](https://wordpress.org/support/users/freedom667/).

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

The topic ‘remove levels with php code’ is closed to new replies.

 * ![](https://ps.w.org/restrict-user-access/assets/icon-256x256.png?rev=1815922)
 * [Restrict User Access - Ultimate Membership & Content Protection](https://wordpress.org/plugins/restrict-user-access/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/restrict-user-access/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/restrict-user-access/)
 * [Active Topics](https://wordpress.org/support/plugin/restrict-user-access/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/restrict-user-access/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/restrict-user-access/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [freedom667](https://wordpress.org/support/users/freedom667/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/remove-levels-with-php-code/#post-13305028)
 * Status: resolved