bp_forums_insert_post

Create a new post.

Description

(int|bool) bp_forums_insert_post( (string) $args = '' ); 

Returns (int|bool)

ID of the new post on success, false on failure.

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

  • post_id (int) => 0

    ID of an existing post, if you want to update rather than create. Default: false.

  • topic_id (int) => 0

    ID of the topic to which the post belongs.

  • post_text (string) => ''

    Contents of the post.

array(

    /**
     * Optional. ID of an existing post, if you want to update rather than create. Default: false.
     *
     * @type int
     * @optional
     */
    'post_id' => 0,

    /**
     * ID of the topic to which the post belongs.
     *
     * @type int
     */
    'topic_id' => 0,

    /**
     * Contents of the post.
     *
     * @type string
     * @default ''
     */
    'post_text' => ''
);        


Usage

  1. if ( !function_exists( 'bp_forums_insert_post' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bp-forums-functions.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'post_id' => 0, 
  7. 'topic_id' => 0, 
  8. 'post_text' => '' 
  9. ); 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bp_forums_insert_post($args); 
  13.  

Defined (1)

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

/bp-forums/bp-forums-functions.php  
  1. function bp_forums_insert_post( $args = '' ) { 
  2.  
  3. /** This action is documented in bp-forums/bp-forums-screens.php */ 
  4.  
  5. $defaults = array( 
  6. 'post_id' => false,  
  7. 'topic_id' => false,  
  8. 'post_text' => '',  
  9. 'post_time' => bp_core_current_time(),  
  10. 'poster_id' => bp_loggedin_user_id(), // Accepts ids or names. 
  11. 'poster_ip' => $_SERVER['REMOTE_ADDR'],  
  12. 'post_status' => 0, // Use bb_delete_post() instead. 
  13. 'post_position' => false 
  14. ); 
  15.  
  16. $r = wp_parse_args( $args, $defaults ); 
  17. extract( $r, EXTR_SKIP ); 
  18.  
  19. if ( !$post = bp_forums_get_post( $post_id ) ) 
  20. $post_id = false; 
  21.  
  22. if ( !isset( $topic_id ) ) 
  23. $topic_id = $post->topic_id; 
  24.  
  25. if ( empty( $post_text ) ) 
  26. $post_text = $post->post_text; 
  27.  
  28. if ( !isset( $post_time ) ) 
  29. $post_time = $post->post_time; 
  30.  
  31. if ( !isset( $post_position ) ) 
  32. $post_position = $post->post_position; 
  33.  
  34. if ( empty( $poster_id ) ) 
  35. return false; 
  36.  
  37. return false; 
  38.  
  39. $post_id = bb_insert_post( array( 'post_id' => $post_id, 'topic_id' => $topic_id, 'post_text' => stripslashes( trim( $post_text ) ), 'post_time' => $post_time, 'poster_id' => $poster_id, 'poster_ip' => $poster_ip, 'post_status' => $post_status, 'post_position' => $post_position ) ); 
  40.  
  41. if ( !empty( $post_id ) ) { 
  42.  
  43. /** 
  44. * Fires if there was a new post created. 
  45. * @since 1.0.0 
  46. * @param int $post_id ID of the newly created forum post. 
  47. */ 
  48.  
  49. return $post_id;