Custom form field
-
We are trying to add an additional field, and using this as the code:
https://givewp.com/documentation/developers/how-to-create-custom-form-fields/I’ve added this to our functions.php file and it works, but it shows on all forms…not just the one 7290 form as listed. I only want this text field to show on the 7290 form.
/** * Add Custom Donation Form Fields * * @param $form_id */ function give_myprefix_custom_form_fields( $form_id ) { // Only display for forms with the IDs "754" and "578"; // Remove "If" statement to display on all forms // For a single form, use this instead: // if ( $form_id == 7290) { $forms = array( 7290 ); ?> <div id="give-referral-wrap"> <label class="give-label" for="give-referral"><?php _e( 'Whom are you sponsoring?:', 'give' ); ?> <span class="give-tooltip icon icon-question" data-tooltip="<?php _e( 'Please tell us whom you are sponsoring.', 'give' ) ?>"></span> </label> <textarea class="give-textarea" name="give_referral" id="give-referral"></textarea> </div> <?php // endif; } add_action( 'give_after_donation_levels', 'give_myprefix_custom_form_fields', 10, 1 ); /** * Validate Custom Field * * Check for errors without custom fields * * @param $valid_data * @param $data */ function give_myprefix_validate_custom_fields( $valid_data, $data ) { // Only validate the form with the IDs "754" and "578"; // Remove "If" statement to display on all forms // For a single form, use this instead: // if ( $form_id == 7290) { $forms = array( 7290 ); if ( in_array( $form_id, $forms ) ) { $required_fields['give_referral'] = array( 'error_id' => 'invalid_give_referral', 'error_message' => __( 'Please tell us whom you are sponsoring.', 'give' ), ); } return $required_fields; } add_filter( 'give_donation_form_required_fields', 'give_myprefix_validate_custom_fields', 10, 2 ); /** * Add Field to Payment Meta * * Store the custom field data custom post meta attached to the <code>give_payment</code> CPT. * * @param $payment_id * @param $payment_data * * @return mixed */ function myprefix123_give_donations_save_custom_fields( $payment_id, $payment_data ) { if ( isset( $_POST['give_referral'] ) ) { $message = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['give_referral'] ) ) ); add_post_meta( $payment_id, 'give_referral', $message ); } } add_action( 'give_insert_payment', 'myprefix123_give_donations_save_custom_fields', 10, 2 ); /** * Show Data in Payment Details * * Show the custom field(s) on the payment details page in wp-admin * * @param $payment_meta * @param $user_info */ function give_myprefix_purchase_details( $payment_meta, $user_info ) { // Bounce out if no data for this transaction $give_referral = get_post_meta( $payment_id, 'give_referral', true ); if ( $give_referral ) : ?> <div class="referral-data"> <label><?php _e( 'Referral:', 'give' ); ?></label> <?php echo wpautop( $give_referral ); ?> </div> <?php endif; } add_action( 'give_payment_personal_details_list', 'give_myprefix_purchase_details', 10, 2 );[Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]
The page I need help with: [log in to see the link]
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
The topic ‘Custom form field’ is closed to new replies.