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?
-
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; }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.
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#L228mod_upload_dir()convinces WordPress to use the BP Docs uploads directory:
https://github.com/boonebgorges/buddypress-docs/blob/master/includes/attachments.php#L372You 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; }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.
Yes, you’re right, that’s all that was needed! Thanks, it works.
Ha ha, Boone can read code as well as write it! A+
The topic ‘Attaching to Docs programmatically’ is closed to new replies.