ulgm_custom_group_post_author
Filters the post author displayed for custom groups, allowing modification of the author ID.
add_filter( 'ulgm_custom_group_post_author', $callback, 10, 1 );
Description
Filters the author ID for newly created groups. Developers can use this hook to assign groups to a different author than the current user or the user associated with a license purchase. The filter receives the user ID, the current user ID, and a context string ('license-purchase').
Usage
add_filter( 'ulgm_custom_group_post_author', 'your_function_name', 10, 1 );
Parameters
-
$user(mixed) - This parameter represents the user object that will be set as the author of the group.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Modify the post author for a group created during a license purchase.
*
* This filter allows you to dynamically change the author of a 'groups' post type
* when it's created as part of a license purchase process.
*
* @param int|null $user_id The user ID to be set as the post author.
* @param int $current_user_id The ID of the currently logged-in user.
* @param string $context The context of the group creation (e.g., 'license-purchase').
* @return int The modified user ID to be set as the post author.
*/
add_filter( 'ulgm_custom_group_post_author', function( $user_id, $current_user_id, $context ) {
// Check if the context is indeed a license purchase.
if ( 'license-purchase' === $context ) {
// For example, if you want to assign all license-purchased groups
// to a specific administrative user (e.g., user ID 1), you could do this:
// return 1;
// Or, if you want to ensure the author is the currently logged-in user
// if they are an administrator and the original $user_id is not set or invalid.
if ( ! $user_id || ! current_user_can( 'manage_options' ) ) {
return $current_user_id;
}
// Otherwise, return the original user ID.
return $user_id;
}
// If the context is not 'license-purchase', return the original $user_id.
return $user_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:590
src/classes/woocommerce/woocommerce-license.php:1001
src/classes/helpers/process-manual-group.php:108
$ld_group_args = apply_filters(
'ulgm_insert_group',
array(
'post_type' => 'groups',
'post_status' => 'publish',
'post_title' => $group_title,
'post_content' => '',
'post_author' => apply_filters( 'ulgm_custom_group_post_author', $user->ID, get_current_user_id(), 'license-purchase' ),
)
);
$group_id = wp_insert_post( $ld_group_args );
foreach ( $line_items as $line ) {
$courses_linked = get_post_meta( $line['product_id'], SharedFunctions::$license_meta_field, true );