bp_forums_new_topic

Create a new forum topic.

Description

(object) bp_forums_new_topic( (string) $args = '' ); 

Returns (object)

Details about the new topic, as returned by {@link bp_forums_get_topic_details()}.

Parameters (1)

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

Options

  • topic_title (string) => ''

    Title of the new topic.

  • topic_slug (string) => ''

    Slug of the new topic.

  • topic_text (string) => ''

    Text of the new topic.

  • topic_poster (int) => 0

    ID of the user posting the topic. Default: ID of the logged-in user.

  • topic_poster_name (string) => ''

    Display name of the user posting the topic. Default: fullname of the logged-in user.

  • topic_last_poster (int) => 0

    ID of the user who last posted to the topic. Default: ID of the logged-in user.

  • topic_last_poster_name (string) => ''

    Display name of the user who last posted to the topic. Default: fullname of the logged-in user.

  • topic_start_time (string) => ''

    Date/time when the topic was created. Default: the current time, as reported by bp_core_current_time().

  • topic_time (string) => ''

    Date/time when the topic was created. Default: the current time, as reported by bp_core_current_time().

  • topic_open (int) => 0

    Whether the topic is open. Default: 1 (open).

  • topic_tags (array|string|bool) => ''

    Array or comma-separated list of topic tags. False to leave empty. Default: false.

array(

    /**
     * Title of the new topic.
     *
     * @type string
     * @default ''
     */
    'topic_title' => '',

    /**
     * Slug of the new topic.
     *
     * @type string
     * @default ''
     */
    'topic_slug' => '',

    /**
     * Text of the new topic.
     *
     * @type string
     * @default ''
     */
    'topic_text' => '',

    /**
     * ID of the user posting the topic. Default: ID of the logged-in user.
     *
     * @type int
     */
    'topic_poster' => 0,

    /**
     * Display name of the user posting the topic. Default: 'fullname' of the logged-in user.
     *
     * @type string
     * @default ''
     */
    'topic_poster_name' => '',

    /**
     * ID of the user who last posted to the topic. Default: ID of the logged-in user.
     *
     * @type int
     */
    'topic_last_poster' => 0,

    /**
     * Display name of the user who last posted to the topic. Default: 'fullname' of the logged-in
     * user.
     *
     * @type string
     * @default ''
     */
    'topic_last_poster_name' => '',

    /**
     * Date/time when the topic was created. Default: the current time, as reported by
     * bp_core_current_time().
     *
     * @type string
     * @default ''
     */
    'topic_start_time' => '',

    /**
     * Date/time when the topic was created. Default: the current time, as reported by
     * bp_core_current_time().
     *
     * @type string
     * @default ''
     */
    'topic_time' => '',

    /**
     * Whether the topic is open. Default: 1 (open).
     *
     * @type int
     */
    'topic_open' => 0,

    /**
     * Array or comma-separated list of topic tags. False to leave empty. Default: false.
     *
     * @type array|string|bool
     * @default ''
     */
    'topic_tags' => ''
);        


Usage

  1. if ( !function_exists( 'bp_forums_new_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bp-forums-functions.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'topic_title' => '', 
  7. 'topic_slug' => '', 
  8. 'topic_text' => '', 
  9. 'topic_poster' => 0, 
  10. 'topic_poster_name' => '', 
  11. 'topic_last_poster' => 0, 
  12. 'topic_last_poster_name' => '', 
  13. 'topic_start_time' => '', 
  14. 'topic_time' => '', 
  15. 'topic_open' => 0, 
  16. 'topic_tags' => '' 
  17. ); 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = bp_forums_new_topic($args); 
  21.  

Defined (1)

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

/bp-forums/bp-forums-functions.php  
  1. function bp_forums_new_topic( $args = '' ) { 
  2. $bp =buddypress); 
  3.  
  4. /** This action is documented in bp-forums/bp-forums-screens.php */ 
  5.  
  6. $r = wp_parse_args( $args, array( 
  7. 'topic_title' => '',  
  8. 'topic_slug' => '',  
  9. 'topic_text' => '',  
  10. 'topic_poster' => bp_loggedin_user_id(), // Accepts ids. 
  11. 'topic_poster_name' => $bp->loggedin_user->fullname, // Accept names. 
  12. 'topic_last_poster' => bp_loggedin_user_id(), // Accepts ids. 
  13. 'topic_last_poster_name' => $bp->loggedin_user->fullname, // Accept names. 
  14. 'topic_open' => 1,  
  15. 'topic_tags' => false, // Accepts array or comma delimited. 
  16. 'forum_id' => 0 // Accepts ids or slugs. 
  17. ) ); 
  18. extract( $r, EXTR_SKIP ); 
  19.  
  20. $topic_title = strip_tags( $topic_title ); 
  21.  
  22. if ( empty( $topic_title ) || !strlen( trim( $topic_title ) ) ) 
  23. return false; 
  24.  
  25. if ( empty( $topic_poster ) ) 
  26. return false; 
  27.  
  28. if ( bp_is_user_inactive( $topic_poster ) ) 
  29. return false; 
  30.  
  31. if ( empty( $topic_slug ) ) 
  32. $topic_slug = sanitize_title( $topic_title ); 
  33.  
  34. if ( !$topic_id = bb_insert_topic( array( 'topic_title' => stripslashes( $topic_title ), 'topic_slug' => $topic_slug, 'topic_poster' => $topic_poster, 'topic_poster_name' => $topic_poster_name, 'topic_last_poster' => $topic_last_poster, 'topic_last_poster_name' => $topic_last_poster_name, 'topic_start_time' => $topic_start_time, 'topic_time' => $topic_time, 'topic_open' => $topic_open, 'forum_id' => (int) $forum_id, 'tags' => $topic_tags ) ) ) 
  35. return false; 
  36.  
  37. // Now insert the first post. 
  38. if ( !bp_forums_insert_post( array( 'topic_id' => $topic_id, 'post_text' => $topic_text, 'post_time' => $topic_time, 'poster_id' => $topic_poster ) ) ) 
  39. return false; 
  40.  
  41. /** 
  42. * Fires after a new forum topic has been created. 
  43. * @since 1.0.0 
  44. * @param int $topic_id ID of the newly created topic post. 
  45. */ 
  46.  
  47. return $topic_id;