Title: Attaching to Docs programmatically
Last modified: July 13, 2018

---

# Attaching to Docs programmatically

 *  [itsnotyouitsme](https://wordpress.org/support/users/itsnotyouitsme/)
 * (@itsnotyouitsme)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/)
 * Great plug-in! I am able to create docs using the BP_Docs_Query save method, 
   but how do I go about adding an attachment programmatically?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Boone Gorges](https://wordpress.org/support/users/boonebgorges/)
 * (@boonebgorges)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10489547)
 * Hello! The nature of WP’s attachment system makes it pretty tricky to do. Here’s
   a snippet from a migration project I recently did that should point you in the
   right direction. You can see near the top that you’ve got to pass the doc_id,
   file_path, file_name, and date_created to the method.
 *     ```
       	protected function add_doc_attachment( $args ) {
       		$doc_id = $args['doc_id'];
       		$file_path = $args['file_path'];
       		$file_name = $args['file_name'];
       		$date_created = $args['date_created'];
   
       		buddypress()->bp_docs->attachments->set_doc_id( $doc_id );
   
       		$tmpfname = wp_tempnam( $file_name );
       		copy( $file_path, $tmpfname );
       		$file = array(
       			'error' => null,
       			'tmp_name' => $tmpfname,
       			'size' => filesize( $file_path ),
       			'name' => $file_name,
       			'type' => pathinfo( $file_path, PATHINFO_EXTENSION ),
       		);
   
       		$overrides = array(
       			'test_form' => false,
       			'test_size' => false,
       			'test_type' => false,
       		);
   
       		$sideloaded = wp_handle_sideload( $file, $overrides );
   
       		if ( isset( $sideloaded['error'] ) ) {
       			var_dump( $sideloaded ); die();
       		}
   
       		$attachment = array(
       			'post_date' => $date_created,
       			'post_mime_type' => $sideloaded['type'],
       			'post_title' => basename( $tmpfname ),
       			'post_content' => '',
       			'post_status' => 'inherit',
       			'post_parent' => $doc_id,
       		);
   
       		$attachment_id = wp_insert_attachment( $attachment, $sideloaded['file'] );
       		$attach_data = wp_generate_attachment_metadata( $attachment_id, $sideloaded );
       		wp_update_attachment_metadata( $attachment_id, $attach_data );
   
       		buddypress()->bp_docs->attachments->set_doc_id( 0 );
   
       		return $attachment_id;
       	}
       ```
   
 *  Thread Starter [itsnotyouitsme](https://wordpress.org/support/users/itsnotyouitsme/)
 * (@itsnotyouitsme)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10489712)
 * I have written functions to add attachments, but what about adding docs to groups
   in which there is protected access? I don’t see that accounted for here, is it?
   I can’t use the standard WP method because they will just be put in the upload
   folder. I think the pieces I am missing are the functions for creating the folder
   under bp-attachments and then creating the .htaccess file. Thanks.
 *  Plugin Author [David Cavins](https://wordpress.org/support/users/dcavins/)
 * (@dcavins)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10490891)
 * Check out how attachments are handled in the plugin for your answers:
 * `create_htaccess()` builds the htaccess:
    [https://github.com/boonebgorges/buddypress-docs/blob/master/includes/attachments.php#L228](https://github.com/boonebgorges/buddypress-docs/blob/master/includes/attachments.php#L228)
 * `mod_upload_dir()` convinces WordPress to use the BP Docs uploads directory:
   
   [https://github.com/boonebgorges/buddypress-docs/blob/master/includes/attachments.php#L372](https://github.com/boonebgorges/buddypress-docs/blob/master/includes/attachments.php#L372)
 * You can add some code to your import/create script (I put mine in a class), something
   like this
 *     ```
       public function create_doc() {
       	// In create function
       	add_filter( 'upload_dir', array( $this, 'mod_upload_dir' ) );
       	// Sideload logic
       }
   
       public function mod_upload_dir( $uploads ) {
       	$subdir = '/bp-attachments/' . $this->new_doc_id;
   
       	$uploads['subdir'] = $subdir;
       	$uploads['path'] = $uploads['basedir'] . $subdir;
       	$uploads['url'] = $uploads['baseurl'] . '/bp-attachments/' . $this->new_doc_id;
   
       	return $uploads;
       }
       ```
   
 *  Plugin Author [Boone Gorges](https://wordpress.org/support/users/boonebgorges/)
 * (@boonebgorges)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10491443)
 * If I recall correctly, buddypress-docs will automatically handle the group-association,
   directory mods, privacy, etc, as long as you have done this:
 *     ```
       buddypress()->bp_docs->attachments->set_doc_id( $doc_id );
       ```
   
 * That triggers all of the filters in attachments.php. So I don’t believe that 
   anything else is necessary.
 *  Thread Starter [itsnotyouitsme](https://wordpress.org/support/users/itsnotyouitsme/)
 * (@itsnotyouitsme)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10492038)
 * Yes, you’re right, that’s all that was needed! Thanks, it works.
 *  Plugin Author [David Cavins](https://wordpress.org/support/users/dcavins/)
 * (@dcavins)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10492667)
 * Ha ha, Boone can read code as well as write it! A+

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Attaching to Docs programmatically’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/buddypress-docs.svg)
 * [BuddyPress Docs](https://wordpress.org/plugins/buddypress-docs/)
 * [Support Threads](https://wordpress.org/support/plugin/buddypress-docs/)
 * [Active Topics](https://wordpress.org/support/plugin/buddypress-docs/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/buddypress-docs/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/buddypress-docs/reviews/)

 * 6 replies
 * 3 participants
 * Last reply from: [David Cavins](https://wordpress.org/support/users/dcavins/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/attaching-to-docs-programmatically/#post-10492667)
 * Status: not resolved