• Hi,

    I’m hoping someone else might be able to shed light on this. In particular this could be useful for anyone else also using PMPro and Learndash.

    I’d like to automatically raise a member by one level upon completion of a course. My PHP skills are by *no* means excellent, and to be perfectly honest this code breaks my site. But I’ll put it out there, because any assistance would be of great help!

    $current_user = wp_get_current_user();
    global $user_id = $current_user->ID;
    
    add_action('learndash_course_completed', function($data) {
    //get membership level, then ++ it
    if (pmpro_hasMembershipLevel()) {
    $membership_level = $current_user->membership_id;
    if(pmpro_hasMembershipLevel($membership_level))
    {
    $membership_level++;
    pmpro_changeMembershipLevel($membership_level, $user_id);
    }
    }
    
    }, 5, 1);

    I’m not sure if my problem is that I don’t understand how the $data in “learndash_course_completed” works (have posted in LD for thsi also). In my experience with creating functions in wordpress, you write the hook and function separately…

    https://wordpress.org/plugins/paid-memberships-pro/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey there,

    It looks like the $data passed is an array containing the WP_User object, WP_Post object for the course, and the membership progress – You can see how the array is defined when the hook is called here:
    do_action('learndash_course_completed', array( 'user' => $current_user, 'course' => get_post($course_id), 'progress' => $course_progress));

    Does that make sense?

    Thanks,
    Jess

    Thread Starter bridieamelia

    (@bridieamelia)

    Hey Jess,

    Thanks for the input! having had some assistance from the good people at Learndash, I’ve tweaked the code a bit to come up with the following. It’s not using PMPro now, but changing course access level upon course completion:

    add_action('learndash_course_completed', function($data) {
    $course_id = $data["course"]->ID;
    $user_id = $data["user"]->ID;
    $next_course_id = get_post_meta($course_id, "next_course_id", true);
    	if(!empty($next_course_id)) {
    	$next_course_id++;
    	ld_update_course_access($user_id, $next_course_id, $remove = false);
    	}
    }, 5, 1);

    So yes – it pulls the user and course data. Unfortunately I still don’t have it working, although thinking I’m right in just dropping this in the functions.php.

    Just out of interest – are you in Aus/NZ? Always nice to find people who reply in similar timezones 🙂
    Bridie

    Anonymous functions like that don’t work on all setups so you might want to try wrapping your code in its own function instead, if you’re not sure and just dropping in the code from somewhere else, but at this point since PMPro is not involved it sounds like the LD team would be able to help more.

    No, I’m in USA, but I would really love to do some traveling over there!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Raise membership level upon course completion (Learndash)’ is closed to new replies.