3

Currently, LearnDash has a function (to be added to functions.php) that allows you to auto enroll a specific user in a course. I was wondering if a simple function could be added to my theme's function file and change this from user_id to a user ROLE? That way every user in that role is enrolled. Here is the starting point: (found in the dev section on Learndash)

 //* To enroll user to course:
ld_update_course_access($user_id, $course_id, $remove = false);

I have tried this:

//* Add users to course by role
ld_update_course_access($role_name = Subscriber, $course_id = 949, $remove = false);

On the "edit course" page editor I now see "1,0,12,Subscriber" inside the "course access list" but it doesn't actually work. Obviously, that access list is working with users only.

My thought process is creating a function that will: 1) Get user IDs from user role "My-Custom-Role" 2) Return IDs and update course access.

Is something like this possible?

jimmyjames
  • 55
  • 1
  • 9

2 Answers2

4

Yep, totally possible. The get_users() function allows you to get a list of users by role. See: https://codex.wordpress.org/Function_Reference/get_users

For example:

$users = get_users( [ 'role__in' => [ 'subscriber', 'author' ] ] );

foreach ( $users as $user ) {
    ld_update_course_access( $user->ID, 949, false );
}
Linnea Huxford
  • 430
  • 2
  • 10
  • Thanks for the direction @Linnea Huxford however I'm still stuck, as I'm now returning the array (course access list now reads "1,0,12,array") but I don't seem to be generating comma separated values. Here's what I used `$users = get_users( [ 'role__in' => [ 'subscriber', 'author' ] ] ); foreach ( $users as $user ) { echo ld_update_course_access($user->user_id, $course_id = 949, $remove = false); }` I also read that I may be generating a rather large query... is there a way to only query the "ID"? – jimmyjames Sep 09 '16 at 01:15
  • I've updated my answer with a code sample. I think you're close, but when calling a function in PHP, you just put the value of the parameter. It's incorrect to put and equal sign and then the value. – Linnea Huxford Sep 09 '16 at 13:00
  • Another concern is that you will have to figure out a way to only have this code run once. You certainly don't want to do this with every page load. I'd look into saving an option or transient that you can check the value of before running this code. Or, you could write up a CLI command that does this and then only run it once. – Linnea Huxford Sep 09 '16 at 13:04
  • I'm still verrrrrrry new to functions but it looks like writing this as an option or a transient will be ideal. I'll play with it for a while. Just wanted to add a note that your code worked. – jimmyjames Sep 12 '16 at 05:29
1

I worked with the development team and came up with a different although incomplete solution, so I've marked Linnea's as correct, because it works as asked in the question. This solution goes through their access hook sfwd_lms_has_access, however the "course list" never gets updated so a user is not officially "enrolled" until they start the course. By this I mean, you wont see them enrolled in the course on their profile, but if they start a lesson, it all of a sudden shows up! Thought it might help to post here in case it may help anyone as a starting point.

add_filter( 'sfwd_lms_has_access', function( $return, $post_id, $user_id ) {

        if ( empty( $user_id ) ) {

                $user_id = get_current_user_id();

        }

if(empty($user_id))

return $return;

$course_id = learndash_get_course_id( $post_id );

$allowed_course_ids = array( 949, 1135 );

if( !in_array($course_id, $allowed_course_ids))

return $return;

if(user_can($user_id, "3a_textbook"))

return true;

if(user_can($user_id, "subscriber"))

return true;

return $return;

}, 10, 3 );
jimmyjames
  • 55
  • 1
  • 9
  • Does this still work in version 3? If so does it only run once as she cautioned earlier? – Shane Nov 15 '19 at 16:40
  • 1
    @Shane I believe so. I'm not using it anymore, as I'm enrolling through a different method. Here is the "official" code on bitbucket. https://bitbucket.org/snippets/learndash/6ogzo/enroll-courses-based-on-user-role-or – jimmyjames Nov 16 '19 at 17:23