// Hook into the PeepSo group creation process
add_action('peepso_action_group_create', 'create_bbpress_forum_on_peepso_group_creation', 10, 1);
/**
* Function to create a bbPress forum when a PeepSo group is created
*
* @param PeepSoGroup $group The PeepSo group object that was just created
*/
function create_bbpress_forum_on_peepso_group_creation($group) {
// Ensure that $group is an instance of PeepSoGroup to avoid errors
if (!($group instanceof PeepSoGroup)) {
return; // Exit the function if $group is not a PeepSoGroup instance
}
// Prepare the data for the new bbPress forum
$forum_data = array(
'post_title' => $group->name, // Set the title of the forum to the name of the PeepSo group
'post_content' => $group->description, // Optionally set the forum description to the PeepSo group description
'post_status' => 'publish', // Publish the forum immediately
'post_type' => bbp_get_forum_post_type(), // Set the post type to 'forum' for bbPress
);
// Insert the new forum into the WordPress database and get the forum ID
$forum_id = wp_insert_post($forum_data);
// Check if there was an error inserting the forum
if (is_wp_error($forum_id)) {
// Log the error message if forum creation fails
error_log('Failed to create bbPress forum: ' . $forum_id->get_error_message());
return; // Exit the function if there was an error
}
// Save the PeepSo group ID as a meta key in the newly created bbPress forum
update_post_meta($forum_id, 'peepso_bbpress_forum_group', $group->id);
// Save the bbPress forum ID as a meta key in the PeepSo group post meta
update_post_meta($group->id, 'peepso_bbpress_group_forum', $forum_id);
// Optionally log success or perform additional actions if needed
}
Comments