Skip to content

Instantly share code, notes, and snippets.

@iworks
Created February 6, 2017 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iworks/4848d61ae6e9582e02f2dd50fa9ce7fb to your computer and use it in GitHub Desktop.
Save iworks/4848d61ae6e9582e02f2dd50fa9ce7fb to your computer and use it in GitHub Desktop.
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