How to auto add courses on sign up

NOTE (wplms v 2.3+) : Tip no longer required. Enable Setting : WP Admin – LMS – Settings – Assign Free courses to students on account activation

1. Go to WP Admin -> Plugins -> Editor -> WPLMS Customizer -> wplms-Customizer.php
2. Add following code in at the bottom of the file:

PHP Code:
add_action('bp_core_activated_user','wplms_activate_free_courses',99); // Runs when user activates his account.
add_action('user_register','wplms_activate_free_courses',99,1);
function wplms_activate_free_courses($user_id = NULL){
if(empty($user_id)){
   if(!is_user_logged_in())
   return;
 
  $user_id = get_current_user_id();
}

   if ( bp_account_was_activated() ){ // Checks if the Account was activated
      $args = array(
        'post_type' => 'course',
        'post_per_page' => -1,
        'meta_query'=>array(
        array(
             'key' => 'vibe_course_free',
             'value' => 'S',
             'compare' => '=',
             'type' => 'CHAR'
            )
        )      
     );
   $free_courses = new WP_Query($args);
   if($free_courses->have_posts()){
     while($free_courses->have_posts()){
         the_post();
        $course_id = get_the_ID();
        bp_course_add_user_to_course($user_id,$course_id);
     }
   }
    wp_reset_postdata();
  }
}

For custom courses:

function wplms_activate_free_courses(){
if(!is_user_logged_in())
return;
 
$user_id = get_current_user_id();
$course_ids = array(15,68,78); //Array of course ids you want user to get access to on account creation.
 
foreach($course_ids as $course_id)
bp_course_add_user_to_course($user_id,$course_id);
 
}

 

Was this article helpful?

Related Articles