Filter
uncanny-automator
uo_create_new_group_status
Filters the status for newly created LearnDash groups, allowing customization before they are published.
add_filter( 'uo_create_new_group_status', $callback, 10, 1 );
Description
This filter hook allows developers to modify the post status for newly created LearnDash groups. It fires just before a new LearnDash group is inserted into the database, enabling developers to dynamically set the group's status. The default status is 'publish'.
Usage
add_filter( 'uo_create_new_group_status', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example filter for 'uo_create_new_group_status' to set a custom post status for newly created groups.
*
* This function demonstrates how to hook into the 'uo_create_new_group_status' filter
* to change the default 'publish' status of a group post to something else, for example, 'pending'
* for moderation or a custom status like 'draft_group'.
*
* @param string $status The current post status, defaults to 'publish'.
* @return string The modified post status.
*/
add_filter( 'uo_create_new_group_status', 'my_custom_group_creation_status', 10, 1 );
function my_custom_group_creation_status( $status ) {
// For demonstration, let's change the status to 'draft' if it's currently 'publish'.
// In a real-world scenario, you might check other conditions or have a dedicated custom status.
if ( 'publish' === $status ) {
// You could also set a custom status like 'draft_group' if you've registered it.
// For this example, we'll just use 'draft'.
return 'draft';
}
// If the status is not 'publish', return it as is.
return $status;
}
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/integrations/learndash/actions/ld-creategroup.php:172
src/integrations/uncanny-groups/actions/uog-createuncannygroup.php:222
public function create_group( $user_id, $action_data, $recipe_id, $args ) {
$uo_group_title = Automator()->parse->text( $action_data['meta']['LDGROUPTITLE'], $recipe_id, $user_id, $args );
$uo_group_courses = Automator()->parse->text( $action_data['meta']['LDGROUPCOURSES'], $recipe_id, $user_id, $args );
$group_leader_role_assignment = Automator()->parse->text( $action_data['meta']['GROUP_LEADER_ROLE_ASSIGNMENT'], $recipe_id, $user_id, $args );
$user = get_user_by( 'ID', $user_id );
if ( ! $user ) {
$error_message = esc_html__( 'User not found.', 'uncanny-automator' );
$action_data['complete_with_errors'] = true;
Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );
return;
}
switch ( trim( $group_leader_role_assignment ) ) {
case 'add':
$user->add_role( 'group_leader' );
break;
case 'replace':
$user->set_role( 'group_leader' );
break;
}
$group_title = $uo_group_title;
$ld_group_args = array(
'post_type' => 'groups',
'post_status' => apply_filters( 'uo_create_new_group_status', 'publish' ),
'post_title' => $group_title,
'post_content' => '',
'post_author' => $user_id,
);
$group_id = wp_insert_post( $ld_group_args );
if ( is_wp_error( $group_id ) ) {
$error_message = $group_id->get_error_message();
$action_data['complete_with_errors'] = true;
Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );
return;
}
ld_update_leader_group_access( $user_id, $group_id );
$group_courses = json_decode( $uo_group_courses );
if ( ! empty( $group_courses ) ) {
foreach ( $group_courses as $course_id ) {
ld_update_course_group_access( (int) $course_id, (int) $group_id, false );
$transient_key = 'learndash_course_groups_' . $course_id;
delete_transient( $transient_key );
}
}
$tokens = array(
'GROUP_ID' => $group_id,
'GROUP_COURSES' => $this->get_group_names( $group_id ),
'GROUP_LEADER_EMAILS' => $this->get_group_leaders_email_addresses( $group_id ),
);
if ( Uncanny_Toolkit_Helpers::is_group_sign_up_activated() ) {
$tokens['GROUP_SIGNUP_URL'] = Uncanny_Toolkit_Helpers::get_group_sign_up_url( $group_id );
}
$this->hydrate_tokens( $tokens );
Automator()->complete_action( $user_id, $action_data, $recipe_id );
}