我可以在Learndash课程中创建用于自动注册WP角色的功能吗? [英] Can I create a function for auto-enrolling WP Roles in a Learndash course?

查看:48
本文介绍了我可以在Learndash课程中创建用于自动注册WP角色的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,LearnDash具有一个功能(将添加到functions.php中),该功能可让您自动注册课程中的特定用户.我想知道是否可以将简单函数添加到主题的函数文件中,并将其从user_id更改为用户ROLE?这样,该角色中的每个用户都将被注册.这是起点:(在Learndash的dev部分中找到)

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);

我已经尝试过了:

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

在编辑课程"页面编辑器上,我现在在课程访问列表"中看到"1,0,12,Subscriber",但实际上不起作用.显然,该访问列表仅适用于用户.

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.

我的思考过程正在创建一个函数,该函数将:1)从用户角色我的自定义角色"获取用户ID2)返回ID并更新课程访问权限.

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.

这样可能吗?

推荐答案

是的,完全有可能. get_users()函数允许您按角色获取用户列表.请参阅: https://codex.wordpress.org/Function_Reference/get_users

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

例如:

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

foreach ( $users as $user ) {
    ld_update_course_access( $user->ID, 949, false );
}

这篇关于我可以在Learndash课程中创建用于自动注册WP角色的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆