Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CoursePress: Enroll new registered user to all available courses. Be careful it can break your site if you have a lot of available courses.
<?php
/**
* Plugin Name: Enroll new user!
* Plugin URI: https://premium.wpmudev.org/
* Description: Enroll new registered user to all available courses. Be careful it can break your site if you have a lot of available courses.
* Version: 1.0
* Author: Marcin Pietrzak (Incsub)
* Author URI: https://premium.wpmudev.org/profile/marcin10
* License: GPL2+
*/
add_action('user_register', 'coursepress_custom_enroll_for_all');
function coursepress_custom_enroll_for_all( $student_id ) {
/**
* get post id for anny published courses
*/
$args = array(
'post_type' => CoursePress_Data_Course::get_post_type_name(),
'nopaging' => true,
'status' => array( 'publish' ),
'fields' => 'ids',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$course_id = get_the_ID();
/**
* avoid to add to full courses
*/
if ( CoursePress_Data_Course::is_full( $course_id ) ) {
continue;
}
/**
* enroll student to course
*/
CoursePress_Data_Course::enroll_student( $student_id, $course_id );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.