bp_docs_create_folder

Create a Folder.

Description

(int|bool) bp_docs_create_folder( (array) $args ); 

Returns (int|bool)

ID of the newly created folder on success, false on failure.

Parameters (1)

0. $args (array)
Array of parameters.

Options

  • name (string) => ''

    Name of the folder, for display in the interface.

  • user_id (int) => 0

    ID of the user that the folder is limited to.

array(

    /**
     * Name of the folder, for display in the interface.
     *
     * @type string
     * @default ''
     */
    'name' => '',

    /**
     * Optional. ID of the user that the folder is limited to.
     *
     * @type int
     * @optional
     */
    'user_id' => 0
);        


Usage

  1. if ( !function_exists( 'bp_docs_create_folder' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-docs/includes/addon-folders.php'; 
  3.  
  4. // Array of parameters. 
  5. $args = array( 
  6. 'name' => '', 
  7. 'user_id' => 0 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_docs_create_folder($args); 
  12.  

Defined (1)

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

/includes/addon-folders.php  
  1. function bp_docs_create_folder( $args ) { 
  2. $r = wp_parse_args( $args, array( 
  3. 'folder_id' => null,  
  4. 'name' => '',  
  5. 'parent' => null,  
  6. 'group_id' => null,  
  7. 'user_id' => null,  
  8. ) ); 
  9.  
  10. if ( empty( $r['name'] ) ) { 
  11. return false; 
  12.  
  13. // Validate group ID 
  14. if ( ! empty( $r['group_id'] ) ) { 
  15. if ( ! bp_is_active( 'groups' ) ) { 
  16. return false; 
  17.  
  18. $r['group_id'] = intval( $r['group_id'] ); 
  19.  
  20. $group = groups_get_group( array( 
  21. 'group_id' => $r['group_id'],  
  22. ) ); 
  23.  
  24. if ( empty( $group->id ) ) { 
  25. return false; 
  26.  
  27. // Validate post parent 
  28. if ( ! empty( $r['parent'] ) ) { 
  29. $r['parent'] = intval( $r['parent'] ); 
  30.  
  31. $maybe_parent = get_post( $r['parent'] ); 
  32. if ( ! is_a( $maybe_parent, 'WP_Post' ) ) { 
  33. return false; 
  34.  
  35. $post_args = array( 
  36. 'post_type' => 'bp_docs_folder',  
  37. 'post_title' => $r['name'],  
  38. 'post_status' => 'publish',  
  39. 'post_parent' => $r['parent'],  
  40. ); 
  41.  
  42. if ( ! empty( $r['folder_id'] ) ) { 
  43. $post_args['ID'] = intval( $r['folder_id'] ); 
  44.  
  45. $folder_id = wp_insert_post( $post_args ); 
  46.  
  47. // If a group ID was passed, associate with group 
  48. if ( ! empty( $r['group_id'] ) ) { 
  49. $group_term = bp_docs_get_folder_in_item_term( $r['group_id'], 'group' ); 
  50. wp_set_object_terms( $folder_id, $group_term, 'bp_docs_folder_in_group' ); 
  51. } else if ( ! empty( $r['user_id'] ) ) { 
  52. $user_term = bp_docs_get_folder_in_item_term( $r['user_id'], 'user' ); 
  53. wp_set_object_terms( $folder_id, $user_term, 'bp_docs_folder_in_user' ); 
  54.  
  55. if ( is_wp_error( $folder_id ) ) { 
  56. return false; 
  57. } else { 
  58. return intval( $folder_id );