Home / Admin / Add Textbox To Donation Form
Duplicate Snippet

Embed Snippet on Your Site

Add Textbox To Donation Form

Adds a textbox to a donation form - for example, to collect a referral code.

Code Preview
php
<?php
/**
 * Shows how to add a custom text box in a donation form.
 * In this example, we are adding a text box to collect a special referral code that the admin can read later.
 *
 * This snippet only works in Charitable 1.5 or above.
 *
 */
function wpchar_charitable_register_new_text_field() {
    /**
     * Define a new text field.
     */
	
	if ( ! class_exists("Charitable_Donation_Field" ) ) {
		return;
	};
		
    /* Create the Donation Field instance. */
    $field = new Charitable_Donation_Field(
        'my_custom_field',
        array(
            'label'          => __( 'Referral Code' ), // what text shows up in the donation form.
            'data_type'      => 'meta', 
            'value_callback' => false,
            'donation_form'  => array(
                'type'       => 'text',  // the type of field to show in the donation form (text, checkbox, etc.)
                'required'   => false,   // whether the field is required.
                'show_after' => 'phone', // the field to show this one after.
            ), 
            'admin_form'     => true,
            'show_in_meta'   => true,
            'show_in_export' => true,
            'email_tag'      => array( // the tag to use in email notifications (make false to not add this to tags.
                'description' => __( 'The custom field value' ),
            ),
        )
    );
    /* Register it. */
    charitable()->donation_fields()->register_field( $field );
}
add_action( 'init', 'wpchar_charitable_register_new_text_field' );

Comments

Add a Comment