Skip to content

Instantly share code, notes, and snippets.

@ericnicolaas
Last active June 23, 2020 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnicolaas/97f29aabd126a6da3965efcd80327dac to your computer and use it in GitHub Desktop.
Save ericnicolaas/97f29aabd126a6da3965efcd80327dac to your computer and use it in GitHub Desktop.
Adding a custom file field to the campaign form
<?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;
} );
@ericnicolaas
Copy link
Author

ericnicolaas commented Jun 23, 2020

In order to display the file as a link in the admin, you need to configure the base_path and view arguments in the admin_form setting to correspond to a PHP file

For 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 using get_attached_file():

$file_id = $view_args['value'];

echo get_attached_file( $file_id );

@ericnicolaas
Copy link
Author

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