Last active
June 23, 2020 08:41
-
-
Save ericnicolaas/97f29aabd126a6da3965efcd80327dac to your computer and use it in GitHub Desktop.
Adding a custom file field to the campaign form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This function adds a custom text field to the campaign submission | |
* form, as well as the admin campaign editor, campaign emails, and | |
* campaign export. | |
* | |
* Note: This example requires Ambassadors 2+. For an example that worked | |
* on previous version, see the link below. | |
* | |
* @see https://github.com/Charitable/library/blob/master/extensions/ambassadors/legacy/add-text-field.php | |
* | |
* @return void | |
*/ | |
add_action( 'init', function( $fields ) { | |
$fields = charitable()->campaign_fields(); | |
// Create the field. | |
$field = new Charitable_Campaign_Field( | |
'custom_file_field', | |
[ | |
'label' => 'Custom File Field', | |
'data_type' => 'meta', | |
'campaign_form' => [ | |
'required' => false, | |
'type' => 'file', | |
'section' => 'campaign-details', | |
'priority' => 30, | |
], | |
'admin_form' => [ | |
'type' => 'content', | |
'content' => '', | |
'base_path' => trailingslashit( __DIR__ ), | |
'view' => 'my-custom-file-field-view', | |
'priority' => 1, | |
], | |
'show_in_export' => true, | |
'email_tag' => true, | |
] | |
); | |
// Register the field. | |
$fields->register_field( $field ); | |
} ); | |
/** | |
* Get the uploaded file from the $_FILES array and add it to the | |
* meta data to be saved for the campaign. | |
* | |
* @param array $values The meta values. | |
* @param Charitable_Data_Processor $data_processor The data processor. | |
* @return array | |
*/ | |
add_filter( 'charitable_campaign_submission_meta_data', function( $values, $data_processor ) { | |
if ( isset( $_FILES ) && isset( $_FILES['custom_file_field'] ) ) { | |
$attachment_id = $data_processor->upload_attachment( 'custom_file_field', $values['ID'] ); | |
if ( ! is_wp_error( $attachment_id ) ) { | |
$submitted[ 'custom_file_field' ] = $attachment_id; | |
/* Delete the previously upload file. */ | |
if ( ! empty( $value['ID'] ) ) { | |
$old_file = get_user_meta( $values['ID'], 'custom_file_field', true ); | |
if ( ! empty( $old_file ) ) { | |
wp_delete_attachment( $old_file ); | |
} | |
} | |
$values['custom_file_field'] = $attachment_id; | |
} | |
} | |
return $values; | |
}, 10, 2 ); | |
/** | |
* Make the PDF field a permitted field. | |
* | |
* Note that the name of the filter includes 'custom_file_field', which should be | |
* replaced with the field id. If you are defining multiple file fields which should | |
* accept non-image fields, you will need to provide multiple filter functions like | |
* below, with the corresponding field key. | |
* | |
* @param array $mime_types The list of permitted mime types for this field. | |
* @return array | |
*/ | |
add_filter( 'charitable_file_custom_file_field_allowed_mimes', function( $mime_types ) { | |
$mime_types['pdf'] = 'application/pdf'; | |
return $mime_types; | |
} ); |
This is the default set of supported mime types:
$allowed_mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'bmp' => 'image/bmp',
'tif|tiff' => 'image/tiff',
'ico' => 'image/x-icon',
);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In order to display the file as a link in the admin, you need to configure the
base_path
andview
arguments in theadmin_form
setting to correspond to a PHP fileFor example, in the example above, the view file would be expected in the same directory where this function is defined, and would be called
my-custom-file-field-view.php
.In the view file, there is a
$view_args
variable available which is an array. It includes the ID of the uploaded file with$view_args['value']
. You can then display the file path usingget_attached_file()
: