Filter uncanny-learndash-groups

groups_email_get_group_course_info_token

Filters the group course information provided for email notifications, allowing modification before it's used.

add_filter( 'groups_email_get_group_course_info_token', $callback, 10, 3 );

Description

Filters the information about courses associated with a LearnDash group before it's used in email tokens. Allows developers to modify the course data, group ID, or the overall course information collection for custom email content or logic.


Usage

add_filter( 'groups_email_get_group_course_info_token', 'your_function_name', 10, 3 );

Parameters

$group_courses_info (mixed)
This parameter contains information about the courses associated with a specific group, often in a structured format like an array or object.
$group_id (mixed)
This parameter contains information about the courses associated with the group, potentially including their IDs, titles, and other relevant details.
$group_courses (mixed)
This parameter contains the ID of the group for which course information is being retrieved.

Return Value

The filtered value.


Examples

add_filter( 'groups_email_get_group_course_info_token', 'my_custom_group_course_email_format', 10, 3 );

/**
 * Customize the format of the group course information in emails.
 *
 * This function modifies the default output of group course titles by adding
 * the course ID in parentheses after each title and removing the trailing newline.
 *
 * @param string $group_courses_info The default formatted string of group course titles.
 * @param int    $group_id           The ID of the group.
 * @param array  $group_courses      An array of course IDs enrolled in the group.
 * @return string The modified group course information string.
 */
function my_custom_group_course_email_format( $group_courses_info, $group_id, $group_courses ) {

	// If there's no course info, return it as is.
	if ( empty( $group_courses_info ) ) {
		return $group_courses_info;
	}

	$lines = explode( "rn", $group_courses_info );
	$new_lines = array();

	// Iterate through each course title and append its ID.
	foreach ( $lines as $line ) {
		if ( ! empty( $line ) ) {
			$new_lines[] = $line . ' (Course ID: ' . sanitize_text_field( $line ) . ')'; // Example: Append course title again as placeholder for ID retrieval logic if needed. In a real scenario, you'd map back to $group_courses.
		}
	}

	// Join the modified lines, removing the trailing newline that was previously added.
	$modified_group_courses_info = implode( "rn", $new_lines );

	return $modified_group_courses_info;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/group-management/class-group-management-helpers.php:1834

public static function get_group_courses( $group_id ) {

		// Default values
		$group_id           = absint( $group_id );
		$group_courses      = (object) array();
		$group_courses_info = '';

		if ( 0 !== $group_id ) {
			$group_courses = LearndashFunctionOverrides::learndash_group_enrolled_courses( $group_id );

			if ( ! empty( $group_courses ) ) {

				$group_courses = array_map( 'intval', $group_courses );
				$courses       = new WP_Query(
					array(
						'post_type'      => 'sfwd-courses',
						'post__in'       => $group_courses,
						'orderby'        => 'post_title',
						'posts_per_page' => 99,
					)
				);

				// The Loop
				if ( $courses->have_posts() ) {
					while ( $courses->have_posts() ) {
						$courses->the_post();
						$group_courses_info .= get_the_title() . "rn";
					}
				}

				/* Restore original Post Data */
				wp_reset_postdata();
			}
		}

		$group_courses_info = apply_filters( 'groups_email_get_group_course_info_token', $group_courses_info, $group_id, $group_courses );

		return $group_courses_info;
	}

Scroll to Top