/**
* Moves one of the default images from the setup folder into the correct site folder
*
* @param string $setup_file should be the filename of the default file to move
* @param int $post_id should be the ID of the post we want to attach this to
* @return boolean returns true/false
*/
function jb_move_setup_files($setup_file, $post_id){
if(! empty($setup_file)){
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
//location of some default images. You will likely need to change this to your default location
$setup_dir = ABSPATH.'wp-content/uploads/setup/';
// $setup_file should be the path to a file in the uploads/setup directory.
$setup_file = $setup_dir.$setup_file;
// $site_file should be the path to the new site directory.
$site_file = $wp_upload_dir['path'] . '/' . basename( $setup_file );
// COPY this file into the new sites upload dir
copy($setup_file, $site_file);
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $site_file ), null );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $site_file,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $site_file ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $site_file, $post_id );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $site_file );
$success = wp_update_attachment_metadata( $attach_id, $attach_data );
//return the attachment id so we can attach this to whatever we want
if($success){
return $attach_id;
}else{
return false;
}
}else{
//no filename given
return false;
}
}