Filter uncanny-learndash-groups

ulgm_license_group_linked_course_id

Filters the linked course ID for a user group, allowing modification before it's applied.

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

Description

Filters the course ID for a specific license group. Developers can use this hook to modify or replace the course ID associated with a license group before access is updated. It fires within the loop that iterates through linked courses for a group.


Usage

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

Parameters

$course_id (mixed)
This parameter holds the ID of a course that has been linked to a license group.
$group_id (mixed)
This parameter contains the ID of a linked course within the license group.
$this (mixed)
This parameter holds the ID of the license group currently being processed.

Return Value

The filtered value.


Examples

/**
 * Example: Modify the linked course ID for a group.
 *
 * This filter allows you to potentially modify or validate the course ID
 * that is linked to a specific group before its access is updated.
 * For instance, you might want to ensure only published courses are linked
 * or perhaps map an external course ID to a WordPress LearnDash course ID.
 *
 * @param int $course_id  The current course ID being processed.
 * @param int $group_id   The ID of the group for which the course is being linked.
 * @param object $object  The instance of the object processing the group.
 * @return int|false      The modified or original course ID, or false to prevent linking.
 */
add_filter( 'ulgm_license_group_linked_course_id', function( $course_id, $group_id, $object ) {

    // Basic validation: Ensure the course ID is a positive integer.
    if ( ! is_numeric( $course_id ) || absint( $course_id ) !== absint( $course_id ) || absint( $course_id ) <= 0 ) {
        // Log an error or warning if an invalid course ID is encountered.
        error_log( sprintf( 'ULGM: Invalid course ID "%s" encountered for group "%s".', $course_id, $group_id ) );
        return false; // Prevent linking if the course ID is invalid.
    }

    // Example: Check if the course exists and is published.
    $course_post = get_post( $course_id );
    if ( ! $course_post || 'publish' !== $course_post->post_status ) {
        // Log a warning if the course is not published.
        error_log( sprintf( 'ULGM: Course "%s" for group "%s" is not published and will not be linked.', $course_id, $group_id ) );
        return false; // Prevent linking if the course is not published.
    }

    // Example: You could add logic here to map a different course ID
    // if $course_id was intended to be an external reference.
    // For example:
    // $mapped_course_id = your_function_to_map_external_id_to_learndash( $course_id );
    // if ( $mapped_course_id ) {
    //     return $mapped_course_id;
    // }

    // If all checks pass, return the original (or potentially modified) course ID.
    return absint( $course_id );

}, 10, 3 );

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/woocommerce/woocommerce-license.php:1034

do_action( 'wpml_set_element_language_details', $language_args );
		}

		$courses_linked = apply_filters( 'ulgm_license_group_courses_linked_in_order', $courses_linked, $group_id, $this );

		if ( $courses_linked ) {
			foreach ( $courses_linked as $course_id ) {
				$course_id = apply_filters( 'ulgm_license_group_linked_course_id', $course_id, $group_id, $this );

				ld_update_course_group_access( (int) $course_id, (int) $group_id, false );

				$transient_key = 'learndash_course_groups_' . $course_id;

				delete_transient( $transient_key );
			}


Scroll to Top