bbp_insert_forum

A wrapper for wp_insert_post() that also includes the necessary meta values for the forum to function properly.

Description

bbp_insert_forum( (array) $forum_data = array(), (array) $forum_meta = array() ); 

Parameters (2)

0. $forum_data — Optional. (array) => array()
The forum data.
1. $forum_meta — Optional. (array) => array()
The forum meta.

Usage

  1. if ( !function_exists( 'bbp_insert_forum' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/forums/functions.php'; 
  3.  
  4. // The forum data. 
  5. $forum_data = array(); 
  6.  
  7. // The forum meta. 
  8. $forum_meta = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_insert_forum($forum_data, $forum_meta); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/forums/functions.php  
  1. function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { 
  2.  
  3. // Forum 
  4. $forum_data = bbp_parse_args( $forum_data, array( 
  5. 'post_parent' => 0, // forum ID 
  6. 'post_status' => bbp_get_public_status_id(),  
  7. 'post_type' => bbp_get_forum_post_type(),  
  8. 'post_author' => bbp_get_current_user_id(),  
  9. 'post_password' => '',  
  10. 'post_content' => '',  
  11. 'post_title' => '',  
  12. 'menu_order' => 0,  
  13. 'comment_status' => 'closed' 
  14. ), 'insert_forum' ); 
  15.  
  16. // Insert forum 
  17. $forum_id = wp_insert_post( $forum_data ); 
  18.  
  19. // Bail if no forum was added 
  20. if ( empty( $forum_id ) ) { 
  21. return false; 
  22.  
  23. // Forum meta 
  24. $forum_meta = bbp_parse_args( $forum_meta, array( 
  25. 'reply_count' => 0,  
  26. 'topic_count' => 0,  
  27. 'topic_count_hidden' => 0,  
  28. 'total_reply_count' => 0,  
  29. 'total_topic_count' => 0,  
  30. 'last_topic_id' => 0,  
  31. 'last_reply_id' => 0,  
  32. 'last_active_id' => 0,  
  33. 'last_active_time' => 0,  
  34. 'forum_subforum_count' => 0,  
  35. ), 'insert_forum_meta' ); 
  36.  
  37. // Insert forum meta 
  38. foreach ( $forum_meta as $meta_key => $meta_value ) { 
  39. update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value ); 
  40.  
  41. // Return new forum ID 
  42. return $forum_id;