bp_docs_add_doc_to_folder

Add a Doc to a Folder.

Description

bp_docs_add_doc_to_folder( (int) $doc_id, (int) $folder_id, (bool) $append = false ); 

Parameters (3)

0. $doc_id (int)
The doc id.
1. $folder_id (int)
The folder id.
2. $append — Optional. (bool) => false
Whether to append to existing folders, or replace. Default: false (replace).

Usage

  1. if ( !function_exists( 'bp_docs_add_doc_to_folder' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-docs/includes/addon-folders.php'; 
  3.  
  4. // The doc id. 
  5. $doc_id = -1; 
  6.  
  7. // The folder id. 
  8. $folder_id = -1; 
  9.  
  10. // Whether to append to existing folders, or replace. 
  11. // Default: false (replace). 
  12. $append = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = bp_docs_add_doc_to_folder($doc_id, $folder_id, $append); 
  16.  

Defined (1)

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

/includes/addon-folders.php  
  1. function bp_docs_add_doc_to_folder( $doc_id, $folder_id, $append = false ) { 
  2. $doc = get_post( $doc_id ); 
  3.  
  4. if ( is_wp_error( $doc ) || empty( $doc ) || bp_docs_get_post_type_name() !== $doc->post_type ) { 
  5. return false; 
  6.  
  7. $folder = get_post( $folder_id ); 
  8.  
  9. if ( is_wp_error( $folder ) || empty( $folder ) || 'bp_docs_folder' !== $folder->post_type ) { 
  10. return false; 
  11.  
  12. $term_id = bp_docs_get_folder_term( $folder_id ); 
  13.  
  14. // misc error 
  15. if ( ! $term_id ) { 
  16. return false; 
  17.  
  18. $existing_folders = wp_get_object_terms( $doc_id, 'bp_docs_doc_in_folder' ); 
  19.  
  20. // Return false if already in folder 
  21. foreach ( $existing_folders as $existing_folder ) { 
  22. if ( $term_id === $existing_folder->term_id ) { 
  23. return false; 
  24.  
  25. // Merge new folder into old ones 
  26. if ( $append ) { 
  27. $term_ids = ! empty( $existing_folders ) ? wp_parse_id_list( wp_list_pluck( $existing_folders, 'term_id' ) ) : array(); 
  28. $term_ids[] = $term_id; 
  29. } else { 
  30. $term_ids = array( $term_id ); 
  31.  
  32. return (bool) wp_set_object_terms( $doc_id, $term_ids, 'bp_docs_doc_in_folder' );