Last active
July 11, 2019 03:45
-
-
Save ericnicolaas/77c1234e36764377a9c5b01532fd4917 to your computer and use it in GitHub Desktop.
Charitable - Add user fields to campaign Funds Recipient tab, with images displayed as thumbnails.
This file contains hidden or 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 example shows how to add extra fields of information to the Funds Recipient | |
* tab, which is displayed for all campaigns in the admin. | |
* | |
* In the example below, all of the fields in the User Details section of the frontend | |
* campaign form are added to the table. If you only want to add one or two fields, | |
* you can add them as follows: | |
$key = 'field-you-want-to-add'; | |
$data[ 'field-you-want-to-add' ] = array( | |
'label' => __( 'My Field Label', 'your-namespace' ), | |
'value' => isset( $submitted[ $key ] ) ? $submitted[ $key ] : '-' | |
); | |
* NOTE: You must always remember to finish the function with 'return $data;'. | |
*/ | |
/** | |
* Add extra submitted fields to the Funds Recipient tab in the campaign admin page. | |
* | |
* @param array $data | |
* @param Charitable_Campaign $campaign | |
* @return array $data | |
*/ | |
function ed_add_campaign_funding_data( $data, Charitable_Campaign $campaign ) { | |
// Get the data that was submitted when the campaign was added. | |
$submitted = $campaign->get( 'submission_data' ); | |
$campaign_form = new Charitable_Ambassadors_Campaign_Form(); | |
// Go through all user fields and add their value to the list of fields to display. | |
foreach ( $campaign_form->get_user_fields() as $key => $field ) { | |
$data[ $key ] = array( | |
'label' => ( isset( $field[ 'label' ] ) ? $field[ 'label' ] : $key ), | |
'value' => ( isset( $submitted[ $key ] ) ? $submitted[ $key ] : '-' ) | |
); | |
// Replace 'my-picture-field' with the key of the picture field. | |
if ( 'my_picture_field' == $key && '-' != $data[ $key ]['value'] ) { | |
$data[ $key ]['value'] = wp_get_attachment_image( $data[ $key ]['value'] ); | |
} | |
// Replace 'my-terms-field' with the key of the category/tags/terms fields. | |
if ( 'my_terms_field' == $key && '-' != $data[ $key ]['value'] ) { | |
$data[ $key ]['value'] = implode( | |
', ', | |
array_map( | |
function( $term_id ) { | |
$term = get_term( $term_id, 'campaign_category' ); | |
return $term->name; | |
}, | |
explode( ',', $data[ $key ]['value'] ) | |
) | |
); | |
} | |
} | |
return $data; | |
} | |
add_filter( 'charitable_ambassadors_campaign_funds_recipient_data', 'ed_add_campaign_funding_data', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment