Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Thanks for your encouragement!

    There are two ways to add additional custom fields to the donation form:

    1) Our Form Field Manager Add-on:
    https://givewp.com/addons/form-field-manager/

    2) Custom code. We have documentation and working examples available here:
    https://givewp.com/documentation/developers/how-to-create-custom-form-fields/

    Thanks!

    Trishah

    (@trishahdee)

    I’m trying to add a phone field and I am getting this PHP warning under the NAME and EMAIL fields:

    Warning: array_key_exists() expects parameter 2 to be array, null given in /home/name/public_html/wp-content/plugins/give/includes/forms/functions.php on line 333
    />

    This is the code I’ve added to functions.php

    /* 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 == 754) {
      $forms = array( 3450, 3480 ); ?>
        <div id="give-phone-wrap">
          <label class="give-label" for="give-phone"><?php _e( 'Phone:', 'give' ); ?>		<span class="give-tooltip icon icon-question" data-tooltip="<?php _e( 'Please enter your phone number so we can contact you if we have any questions.', 'give' ) ?>"></span>
          </label>
          <textarea class="give-textarea" name="give-phone" id="give-phone"></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 ) {
      // 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 == 754) {
      $forms = array( 3450, 3480 );
      if ( in_array( $form_id, $forms ) ) {
        $required_fields['give_phone'] = array(
          'error_id'      => 'invalid_give_phone',
          'error_message' => __( 'Please tell us your phone number.', '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-phone'] ) ) {
        $message = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['give-phone'] ) ) );
        add_post_meta( $payment_id, 'give-phone', $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_phone = get_post_meta( $payment_id, 'give-phone', true );
            if ( $give_phone ) : ?>
      <div class="referral-data">
        <label><?php _e( 'Phone:', 'give' ); ?></label>
        <?php echo wpautop( $give_phone ); ?>
      </div>
            <?php endif;
     }
    
    add_action( 'give_payment_personal_details_list', 'give_myprefix_purchase_details', 10, 2 );
    

    Can you tell me what I’m doing wrong?

    Also, the $forms array is asking for form id numbers. How do I write this so it applies to any form that is created?

    • This reply was modified 6 months ago by  Trishah.
    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Hmm… perhaps our documentation is a little off. Just as a reference I updated your code based on our custom fields plugin snippet here:
    https://github.com/WordImpress/Give-Snippet-Library/blob/master/form-customizations/custom-fields-plugin.php

    Try this and let me know how it works for you:

    /* 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 == 754) {
    	$forms = array( 3450, 3480 );
    	if ( in_array( $form_id, $forms ) ) { ?>
            <div id="give-phone-wrap">
                <label class="give-label" for="give-phone"><?php _e( 'Phone:', 'give' ); ?> <span
                            class="give-tooltip icon icon-question"
                            data-tooltip="<?php _e( 'Please enter your phone number so we can contact you if we have any questions.', 'give' ) ?>"></span>
                </label>
                <textarea class="give-textarea" name="give-phone" id="give-phone"></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( $required_fields, $form_id ) {
    	// 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 == 754) {
    	$forms = array( 3450, 3480 );
    	if ( in_array( $form_id, $forms ) ) {
    		$required_fields['give_phone'] = array(
    			'error_id'      => 'invalid_give_phone',
    			'error_message' => __( 'Please tell us your phone number.', '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-phone'] ) ) {
    		$message = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['give-phone'] ) ) );
    		add_post_meta( $payment_id, 'give-phone', $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_id ) {
    	// Bounce out if no data for this transaction
    	$give_phone = get_post_meta( $payment_id, 'give-phone', true );
    	if ( $give_phone ) : ?>
            <div class="referral-data">
                <label><?php _e( 'Phone:', 'give' ); ?></label>
    			<?php echo wpautop( $give_phone ); ?>
            </div>
    	<?php endif;
    }
    
    add_action( 'give_payment_personal_details_list', 'give_myprefix_purchase_details', 10, 2 );
    Trishah

    (@trishahdee)

    Yep, that worked 😀 Thank you so much!

    A question and a note…

    Q: How would I set the phone fields to automatically work on every form that is created instead of having the identify the form numbers manually? The client will be adding many different campaigns over time and it would be good if the phone field was just applied to any new forms that are created.

    NOTE: It took me a while to get the input to show up in the correct place on the form using the hierarchy on this page: https://givewp.com/add-content-donation-forms/ It would have been helpful if there were an infographic showing where each of the points are within the form.

    UPDATE: After more testing I discovered that the submitted phone number is not showing in the donation records. I checked and found there is no record for the phone number being saved in the database. I have compared my functions.php code to the plugin code you referenced, and I see no difference that could account for this.

    Please help.

    Thank you

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Hi Trish,

    Here’s revised code that should work as expected:

    /* 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 == 754) {
    	//$forms = array( 3450, 3480 );
    	//if ( in_array( $form_id, $forms ) ) { ?>
            <div id="give-phone-wrap">
                <label class="give-label" for="give-phone"><?php _e( 'Phone:', 'give' ); ?> <span
                            class="give-tooltip icon icon-question"
                            data-tooltip="<?php _e( 'Please enter your phone number so we can contact you if we have any questions.', 'give' ) ?>"></span>
                </label>
                <input class="give-input" name="give-phone" id="give-phone"></input>
            </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( $required_fields, $form_id ) {
    	// 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 == 754) {
    	//$forms = array( 3450, 3480 );
    	//if ( in_array( $form_id, $forms ) ) {
    		$required_fields['give-phone'] = array(
    			'error_id'      => 'invalid_give_phone',
    			'error_message' => __( 'Please tell us your phone number.', '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-phone'] ) ) {
    		$message = implode( "\n", array_map( 'sanitize_text_field', explode( "\n", $_POST['give-phone'] ) ) );
    		add_post_meta( $payment_id, 'give-phone', $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_id ) {
    
    	$givephone = get_post_meta( $payment_id, 'give-phone', true );
    
    	// Bounce out if no data for this transaction
    	if ( $givephone ) : ?>
            <div id="give-engraving-details" class="postbox">
                <h3 class="hndle"><?php esc_html_e( 'Custom Fields', 'give' ); ?></h3>
                <div class="inside" style="padding-bottom:10px;">
                    <strong>Phone:</strong> <?php echo $givephone; ?>
                </div>
            </div>
    	<?php endif;
    }
    
    add_action( 'give_view_order_details_billing_before', 'give_myprefix_purchase_details', 10, 2 );

    The information was being stored to the database physically, but couldn’t be seen correctly because it wasn’t being called correctly.

    Thanks!

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    And to clarify, the “if” statements is what was limiting the phone to just certain forms. Remove those (or comment them out as I have) and it’ll be applied to ALL form.

    Thanks!

    Hello Matt,

    Thank you for the rewrite. It is working now and I can see the phone number in the Admin contributions.

    Two more things..

    #1 The client wants the phone number to be sent in the email that goes to her. How would I add this information to the New Donation Notification email?

    #2 There’s a minor spelling error in your the last section of your code: <h3 class="hndle">

    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    For custom fields, you’ll have to create custom email tags for that as well. Here’s docs on that:
    https://givewp.com/documentation/developers/how-to-add-custom-email-tags/

    You can see the example code in that article and also in our custom field example plugin as well:
    https://github.com/WordImpress/Give-Snippet-Library/blob/master/form-customizations/custom-fields-plugin.php

    Thanks!

    Hello Matt,

    I’ve tried the methods from both links. Both create the {phone} tag for the email. The second one I can’t get to output anything. The first one keeps saying there is no data found. Here is this code. What am I doing wrong?

    /**
     * Adds a Custom "Referral" Tag
     * @description: This function creates a custom Give email template tag
     *
     * @param $payment_id
     */
    function my_custom_prefix_add_sample_referral_tag( $payment_id ) {
      give_add_email_tag( 'phone', 'This tag can be used to output the custom phone field', 'my_custom_prefix_get_donation_referral_data' );
    }
    
    add_action( 'give_add_email_tags', 'my_custom_prefix_add_sample_referral_tag' );
    
    /**
     * Get Donation Referral Data
     *
     * @description Example function that returns Custom field data if present in payment_meta; the example used here is in conjunction with the Give documentation tutorials
     * @param $payment_id
     *
     * @return string|void
     */
    function my_custom_prefix_get_donation_referral_data( $payment_id ) {
    
      $payment_meta = give_get_payment_meta( $payment_id );
      $output       = __( 'No phone data found.', 'give' );
      if ( isset( $payment_meta['give-phone'] ) && ! empty( $payment_meta['give-phone'] ) ) {
        $output = $payment_meta['give-phone'];
      }
    
      return $output;
    }
    
    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Most likely the “if” statement is not being returned as true. Try removing the “if” statement and just show $output = $payment_meta['give-phone']; instead.

    Pro tip: Do a new donations after you make that change. Then go to “Donations > Settings > Emails” and click on the “Preview Email” button and you can see the results live. Then you can keep making changes and just refresh that preview page instead of having to resend new emails all the time.

    Thank you for your reply.

    Now only the phone label is in the email but the phone number is blank. Phone number is still being displayed in the actual order.

    Also, there is only a preview in the Admin for the Order Receipt and not the Order Notification Email.

    • This reply was modified 5 months, 3 weeks ago by  Trishah.
    Plugin Author Matt Cromwell

    (@webdevmattcrom)

    Thanks for reaching out via our contact form. Just for the record, here’s the final code we used for the custom email tag:

    /**
     * Adds a Custom "Phone" Tag
     * @description: This function creates a custom Give email template tag
     *
     * @param $payment_id
     */
    function my_custom_prefix_add_sample_referral_tag( $payment_id ) {
        give_add_email_tag( 'phone', 'This tag can be used to output the custom phone field', 'my_custom_prefix_get_donation_referral_data' );
    }
    
    add_action( 'give_add_email_tags', 'my_custom_prefix_add_sample_referral_tag' );
    
    /**
     * Get Donation Phone Data
     *
     * @description Example function that returns Custom field data if present in payment_meta; the example used here is in conjunction with the Give documentation tutorials
     * @param $payment_id
     *
     * @return string|void
     */
    function my_custom_prefix_get_donation_referral_data( $payment_id ) {
    
        $payment_meta = get_post_meta( $payment_id, 'give-phone', true );
    
        $output = __( 'No phone data found.', 'give' );
    
        if ( ! empty( $payment_meta ) ) {
            $output = $payment_meta;
        }
    
        return $output;
    }

    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!

    Thank you Matt. Review submitted. Mark as resolved.

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.