Filter uncanny-learndash-groups

ulgm_custom_license_post_author

Filters the user ID for the license post author, allowing customization of who is credited.

add_filter( 'ulgm_custom_license_post_author', $callback, 10, 1 );

Description

Filters the user object used to determine the author of a custom license post. Modify this hook to change the user associated with a license, perhaps for role management or ownership tracking, before the post is created.


Usage

add_filter( 'ulgm_custom_license_post_author', 'your_function_name', 10, 1 );

Parameters

$user (mixed)
This parameter holds the user object that represents the author of the license post, which can be modified by the filter.

Return Value

The filtered value.


Examples

/**
 * Filter the post author for custom purchased licenses.
 *
 * This function allows modifying the author of a product post when it's
 * associated with a custom license purchase. It checks if the current user
 * is an administrator, and if so, keeps the original author. Otherwise,
 * it assigns the post author to the current user's ID.
 *
 * @param int $original_author_id The original author ID of the post.
 * @param int $current_user_id The ID of the currently logged-in user.
 * @param string $context The context of the filter ('license-purchase').
 * @return int The author ID to be set for the post.
 */
add_filter( 'ulgm_custom_license_post_author', function( $original_author_id, $current_user_id, $context ) {
	// Check if the context is what we expect for custom license purchases.
	if ( 'license-purchase' === $context ) {
		// If the current user is an administrator, retain the original author.
		// Otherwise, assign the current user as the author.
		if ( user_can( $current_user_id, 'administrator' ) ) {
			return $original_author_id;
		} else {
			return $current_user_id;
		}
	}
	// If the context doesn't match, return the original author ID.
	return $original_author_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-subscription.php:575
src/classes/woocommerce/woocommerce-license.php:985

$group_title = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() . ' ' . $order->get_billing_company();
			$group_title = apply_filters( 'ulgm_group_name', trim( $group_title ), $order );
		}
		if ( 'yes' === get_post_meta( $product_id, '_uo_custom_buy_product', true ) ) {
			wp_update_post(
				array(
					'ID'          => $product_id,
					'post_author' => apply_filters( 'ulgm_custom_license_post_author', $user->ID, get_current_user_id(), 'license-purchase' ),
				)
			);
		}
		if ( ! user_can( $user, 'group_leader' ) && ! user_can( $user, 'administrator' ) ) {
			$user->add_role( 'group_leader' );
		}


Scroll to Top