Donation Form – Need to Re-Order Fields via Code Snippets?
-
Hello Eric,
I found your GitHub code snippets for adding a checkbox to the donation form – thank you for posting that!
I now have several custom text fields and checkboxes, and have loaded them via the Code Snippets plugin, as you suggested.
MY QUESTION — >Is there a way in the code snippets to set the ORDER of the fields?
I see that they are all coming in below the default fields that you include – and I’d like to reorder them slightly (still below the default fields). However, I have not figured out how to set the order within the code snippets. I’m imagining that there’s a spot in the code snippet that can be used to set the order?
Thank you!
-
Hi Beth,
They should be ordered by priority, from lowest to highest, so you can change their order by changing their priority setting. For example:
$fields[ 'national_id_number' ] = array( 'label' => __( 'National ID Number', 'your-namespace' ), 'type' => 'text', 'priority' => 24, 'value' => $form->get_user_value( 'donor_national_id_number' ), 'required' => true, 'data_type' => 'user' );
The priority of that field is 24. If you want it to come sooner, change it to a lower number. It’ll depend on the priority of your other fields.
Cheers,
EricHi Eric – yes, I tried this and it works if all the fields in question are text fields. But there isn’t anywhere to add the ‘priority’ setting in the code for adding a checkbox field. Can you help with that?
What’s happening is that my text fields are lining up above my checkbox fields and I can’t re-order any checkbox fields above any of the text fields – nor can I reorder the checkbox fields themselves.
Thank you!
Hi Beth,
Could you post your code here?
Cheers,
EricHi Eric,
Sorry for delay – Here’s the code for 4 fields. I want them to appear in the order they are coded below:
Text: Dedicate my Donation
Checkbox: Send Ack to the person or org for dedication
Text: Acknowledge my Donation Publicly
Checkbox: No, do not acknowledge my donation publiclyHowever, what happens is the two text fields are appearing first, and then the two checkboxes, out of order from each other. Here’s the code:
/**
* Collect the donor’s Dedication wishes in the donation form.
*
* @param array[] $fields
* @param Charitable_Donation_Form $form
* @return array[]
*/
function ed_collect_dedication( $fields, Charitable_Donation_Form $form ) {
$fields[ ‘donor_dedication’ ] = array(
‘label’ => __( ‘Please dedicate my donation to (in honor of, in memoriam, etc.):’, ‘your-namespace’ ),
‘type’ => ‘text’,
‘priority’ => 22,
‘value’ => $form->get_user_value( ‘donor_dedication’ ),
‘required’ => false,
‘data_type’ => ‘user’
);
return $fields;
}
add_filter( ‘charitable_donation_form_user_fields’, ‘ed_collect_dedication’, 10, 2 );
/**
* Display the Dedication Information in the admin donation details box.
*
* @param array[] $meta
* @param Charitable_Donation $donation
* @return array[]
*/
function ed_show_donor_dedication_in_admin( $meta, $donation ) {
$donor_data = $donation->get_donor_data();
$meta[ ‘donor_dedication’ ] = array(
‘label’ => __( ‘Dedication’, ‘your-namespace’ ),
‘value’ => $donor_data[ ‘donor_dedication’ ]
);
return $meta;
}
add_filter( ‘charitable_donation_admin_meta’, ‘ed_show_donor_dedication_in_admin’, 10, 2 );/**
* Add a checkbox on the donation form to ask the donor whether they would like to have their donation DEDICATION acknowledged. Opt-IN Checkbox.
*
* @return void
*/
function ed_add_donation_dedication_ack_opt_in_checkbox() {
$ticked = isset( $_POST[ ‘donation_dedication_ack_optin’ ] ) && $_POST[ ‘donation_dedication_ack_optout’ ];
?><div id=”charitable_field_donation_dedication_ack_optin” class=”charitable-form-field charitable-form-field-checkbox”>
<input type=”checkbox” name=”donation_dedication_ack_optin” value=”1″ <?php checked( $ticked ) ?> />
<label for=”charitable_field_donation_dedication_ack_optin”><?php _e( ‘Please send an acknowledgement to the individual or organization to whom I am dedicating my donation (contact information included above).’, ‘your-namespace’ ) ?></label>
</div>
<?php
}
add_filter( ‘charitable_donation_form_donor_fields_after’, ‘ed_add_donation_dedication_ack_opt_in_checkbox’ );
/**
* Add donation_ack_optin to the list of meta fields to be saved.
*
* @param mixed[] $meta
* @param int $donation_id
* @param Charitable_Donation_Processor $processor
* @return mixed[]
*/
function ed_save_donation_dedication_ack_opt_in_meta_field( $meta, $donation_id, Charitable_Donation_Processor $processor ) {
$meta[ ‘donation_dedication_ack_optin’ ] = $processor->get_donation_data_value( ‘donation_dedication_ack_optin’ );
return $meta;
}
add_filter( ‘charitable_donation_meta’, ‘ed_save_donation_dedication_ack_opt_in_meta_field’, 10, 3 );
/**
* The value for donation_dedication_ack_optout should always be either 1 or 0.
*
* @return int
*/
function ed_save_submitted_value_donation_dedication_ack_opt_in( $values ) {
$values[ ‘donation_dedication_ack_optin’ ] = isset( $_POST[ ‘donation_dedication_ack_optin’ ] ) && $_POST[ ‘donation_dedication_ack_optin’ ] ? 1 : 0;
return $values;
}
add_filter( ‘charitable_donation_values’, ‘ed_save_submitted_value_donation_dedication_ack_opt_in’ );/**
* Collect the donor’s Public Acknowledgement wishes in the donation form.
*
* @param array[] $fields
* @param Charitable_Donation_Form $form
* @return array[]
*/
function ed_collect_donor_ack_name( $fields, Charitable_Donation_Form $form ) {
$fields[ ‘donor_ack_name’ ] = array(
‘label’ => __( ‘Yes, you may acknowledge my donation publicly. Use this name (or names) to acknowledge my donation: (fill-in only if different from name entered above)’, ‘your-namespace’ ),
‘type’ => ‘text’,
‘priority’ => 24,
‘value’ => $form->get_user_value( ‘donor_ack_name’ ),
‘required’ => false,
‘data_type’ => ‘user’
);
return $fields;
}
add_filter( ‘charitable_donation_form_user_fields’, ‘ed_collect_donor_ack_name’, 10, 2 );
/**
* Display the Dedication Information in the admin donation details box.
*
* @param array[] $meta
* @param Charitable_Donation $donation
* @return array[]
*/
function ed_show_donor_ack_name_in_admin( $meta, $donation ) {
$donor_data = $donation->get_donor_data();
$meta[ ‘donor_ack_name’ ] = array(
‘label’ => __( ‘Donor Public Name’, ‘your-namespace’ ),
‘value’ => $donor_data[ ‘donor_ack_name’ ]
);
return $meta;
}
add_filter( ‘charitable_donation_admin_meta’, ‘ed_show_donor_ack_name_in_admin’, 10, 2 );/**
* Add a checkbox on the donation form to ask the donor whether they would like to have the donation acknowledged publicly.
*
* @return void
*/
function ed_add_donation_public_ack_opt_out_checkbox() {
$ticked = isset( $_POST[ ‘donation_ack_optout’ ] ) && $_POST[ ‘donation_ack_optout’ ];
?>
<div id=”charitable_field_donation_ack_optout” class=”charitable-form-field charitable-form-field-checkbox”>
<input type=”checkbox” name=”donation_ack_optout” value=”1″ <?php checked( $ticked ) ?> />
<label for=”charitable_field_donation_ack_optout”><?php _e( ‘No, please do not acknowledge my donation publicly.’, ‘your-namespace’ ) ?></label>
</div>
<?php
}
add_filter( ‘charitable_donation_form_donor_fields_after’, ‘ed_add_donation_public_ack_opt_out_checkbox’ );
/**
* Add donation_ack_optout to the list of meta fields to be saved.
*
* @param mixed[] $meta
* @param int $donation_id
* @param Charitable_Donation_Processor $processor
* @return mixed[]
*/
function ed_save_donation_public_ack_opt_out_meta_field( $meta, $donation_id, Charitable_Donation_Processor $processor ) {
$meta[ ‘donation_ack_optout’ ] = $processor->get_donation_data_value( ‘donation_ack_optout’ );
return $meta;
}
add_filter( ‘charitable_donation_meta’, ‘ed_save_donation_public_ack_opt_out_meta_field’, 10, 3 );
/**
* The value for donation_ack_optout should always be either 1 or 0.
*
* @return int
*/
function ed_save_submitted_value_donation_public_ack_opt_out( $values ) {
$values[ ‘donation_ack_optout’ ] = isset( $_POST[ ‘donation_ack_optout’ ] ) && $_POST[ ‘donation_ack_optout’ ] ? 1 : 0;
return $values;
}
add_filter( ‘charitable_donation_values’, ‘ed_save_submitted_value_donation_public_ack_opt_out’ );
- The topic ‘Donation Form – Need to Re-Order Fields via Code Snippets?’ is closed to new replies.