@ Jonathan
First, adding this field does not require editing the core Connections code, I strongly recommend that you do not do that so you can safely apply updates to Connection as they are released.
The following code adds an upload field which will upload a file into the WordPress Media Library and save the file meta into the entry meta. This pretty basic code and does not do stuff such as removing an existing file from the Media Library if the user uploads a new file, does not remove the file if the entry is deleted, does not limit file types and so on. You’ll need to add the code to take care of those actions and tighten up security.
As you see it is actually pretty simple. Once you know how, of course. More than half the code is the usual code one would use to upload files in WordPress. The remaining code registers the custom field and custom meta box to display the field, the file meta if a file is uploaded to the entry. Really the only Connections specific code is the action hooks needed, the code within the action callbacks is nothing special, and the callback function which actually registers the custom field and metabox.
// Register the custom upload field.
add_action( 'cn_meta_field-resume_upload_field', 'resume_upload_field', 10, 3 );
// Register the metabox and field.
add_action( 'cn_metabox', 'resume_upload_metabox' );
// Since we're using a custom field, we need to add our own sanitization method.
add_filter( 'cn_meta_sanitize_field-resume_upload_field', 'sanitize_resume_upload_field' );
// Render the custom upload field.
function resume_upload_field( $field, $files, $entry ) {
resume_upload_file_meta( $files );
echo '<p><input type="file" name="resume" id="resume" /></p>';
}
// Display the uploaded file meta.
function resume_upload_file_meta( $files ) {
if ( ! empty( $files ) && is_array( $files ) ) {
foreach ( $files as $file ) {
echo '<p><strong>Name:</strong> <input class="large-text" type="text" value="' . esc_attr( $file['name'] ) . '" disabled></p>';
echo '<p><strong>Path:</strong> <input class="large-text" type="text" value="' . esc_attr( $file['file'] ) . '" disabled></p>';
echo '<p><strong>URL:</strong> <input class="large-text" type="text" value="' . esc_attr( $file['url'] ) . '" disabled></p>';
}
}
}
// Render the metabox which will reder the custom upload field.
function resume_upload_metabox() {
$atts = array(
'id' => 'resume-metabox',
'title' => 'Resume',
'context' => 'normal',
'priority' => 'core',
'fields' => array(
array(
'id' => 'resume',
'type' => 'resume_upload_field',
),
),
);
cnMetaboxAPI::add( $atts );
}
// This will pass back an array of the file meta data such as filename, path and URL which
// will be saved into the entry meta.
function sanitize_resume_upload_field( $file_meta ) {
$file_meta = array();
if ( ! empty( $_FILES ) ) {
foreach( $_FILES as $file ) {
if ( is_array( $file ) ) {
$result = upload_resume( $file );
if ( false != $result ) {
$file_meta[] = $result;
}
}
}
}
return $file_meta;
}
// Process the uploaded file.
function upload_resume( $data ) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
}
$upload = wp_handle_upload( $data, array( 'test_form' => false ) );
if ( isset( $upload['error'] ) || isset( $upload['upload_error_handler'] ) ) {
return false;
} else {
$upload['name'] = basename( $upload['file'] );
$attachment = array(
'post_mime_type' => $upload['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $upload['name'] ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $upload['url']
);
wp_insert_attachment( $attachment, $upload['url'] );
return $upload;
}
return false;
}
To have this field show up in Form, go to the Connections : Settings admin page, click the Form and enable the Resume field, drag it in the position you want it and click the save button.
Hope that helps!