• Resolved solwebsolutions

    (@solwebsolutions)


    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.]

    • This topic was modified 8 years, 3 months ago by bdbrown.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Matt Cromwell

    (@webdevmattcrom)

    Hi there,

    You have the “IF” statement commented out. Update your very first function to this:

    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; 
    } 
    

    Then the validate function also has the “IF” statement commented out. Update to this:

    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;
    }

    Thanks!

    Thread Starter solwebsolutions

    (@solwebsolutions)

    Hi!

    When I un-comment out the line on both functions:
    if ( $form_id == 7290) {

    …I get a 500 error on the site.

    Plugin Contributor Matt Cromwell

    (@webdevmattcrom)

    Here’s the full, corrected code:

    /**
     * Add Custom Donation Form Fields
     *
     * @param $form_id
     */ 
    function give_myprefix_custom_form_fields( $form_id ) {
    	?>
    		<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 
    } 
    
    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 ) {
    
    		$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_purc
    Thread Starter solwebsolutions

    (@solwebsolutions)

    Hi…where is the distinction for the 7290 form?
    It’s still showing on both donate forms.

    Please advise…thank you!

    Plugin Contributor Matt Cromwell

    (@webdevmattcrom)

    Apologies, I was thinking of it the opposite way. Here it is with it being shown only on form 7290

    /**
     * Add Custom Donation Form Fields
     *
     * @param $form_id
     */ 
    function give_myprefix_custom_form_fields( $form_id ) {
    
    	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 
    	} 
    } 
    
    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 ) {
    		
    		$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;
     } 
    

    Thanks!

    • This reply was modified 8 years, 3 months ago by Matt Cromwell.
    Thread Starter solwebsolutions

    (@solwebsolutions)

    Thanks so much…that worked!

    Plugin Contributor Matt Cromwell

    (@webdevmattcrom)

    Glad to hear, happy to help!

    If you’re enjoying Give and appreciate our support, we’d love a kind review from you here:
    https://wordpress.org/support/plugin/give/reviews/

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Custom form field’ is closed to new replies.