Advertisement
vapvarun

Function to create a bbPress forum when a PeepSo group is created

Aug 29th, 2024
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | Software | 0 0
  1. // Hook into the PeepSo group creation process
  2. add_action('peepso_action_group_create', 'create_bbpress_forum_on_peepso_group_creation', 10, 1);
  3.  
  4. /**
  5.  * Function to create a bbPress forum when a PeepSo group is created
  6.  *
  7.  * @param PeepSoGroup $group The PeepSo group object that was just created
  8.  */
  9. function create_bbpress_forum_on_peepso_group_creation($group) {
  10.     // Ensure that $group is an instance of PeepSoGroup to avoid errors
  11.     if (!($group instanceof PeepSoGroup)) {
  12.         return; // Exit the function if $group is not a PeepSoGroup instance
  13.     }
  14.  
  15.     // Prepare the data for the new bbPress forum
  16.     $forum_data = array(
  17.         'post_title'    => $group->name, // Set the title of the forum to the name of the PeepSo group
  18.         'post_content'  => $group->description, // Optionally set the forum description to the PeepSo group description
  19.         'post_status'   => 'publish', // Publish the forum immediately
  20.         'post_type'     => bbp_get_forum_post_type(), // Set the post type to 'forum' for bbPress
  21.     );
  22.  
  23.     // Insert the new forum into the WordPress database and get the forum ID
  24.     $forum_id = wp_insert_post($forum_data);
  25.  
  26.     // Check if there was an error inserting the forum
  27.     if (is_wp_error($forum_id)) {
  28.         // Log the error message if forum creation fails
  29.         error_log('Failed to create bbPress forum: ' . $forum_id->get_error_message());
  30.         return; // Exit the function if there was an error
  31.     }
  32.  
  33.     // Save the PeepSo group ID as a meta key in the newly created bbPress forum
  34.     update_post_meta($forum_id, 'peepso_bbpress_forum_group', $group->id);
  35.  
  36.     // Save the bbPress forum ID as a meta key in the PeepSo group post meta
  37.     update_post_meta($group->id, 'peepso_bbpress_group_forum', $forum_id);
  38.  
  39.     // Optionally log success or perform additional actions if needed
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement