Phone Number Field at EDD Checkout
Adds a phone number field to the Easy Digital Downloads checkout, saves it with the order, shows it on the order screen and provides a {checkout_phone} email tag.
Opens WPIDE on
Code
$wpide_edd_phone_required = true; // Require a phone number.
add_action( 'edd_purchase_form_user_info_fields', function () use ( $wpide_edd_phone_required ) {
?>
<p id="edd-phone-wrap">
<label class="edd-label" for="edd-phone"><?php echo esc_html( 'Phone number' ); ?><?php echo $wpide_edd_phone_required ? ' <span class="edd-required-indicator">*</span>' : ''; ?></label>
<input class="edd-input" type="tel" name="edd_phone" id="edd-phone" autocomplete="tel" <?php echo $wpide_edd_phone_required ? 'required' : ''; ?>>
</p>
<?php
} );
if ( $wpide_edd_phone_required ) {
add_filter( 'edd_purchase_form_required_fields', function ( $fields ) {
$fields['edd_phone'] = array(
'error_id' => 'invalid_phone',
'error_message' => 'Please enter your phone number.',
);
return $fields;
} );
}
add_action( 'edd_built_order', function ( $order_id ) {
// phpcs:ignore WordPress.Security.NonceVerification -- EDD verifies the purchase form nonce.
$phone = isset( $_POST['edd_phone'] ) ? sanitize_text_field( wp_unslash( $_POST['edd_phone'] ) ) : '';
if ( '' !== $phone && function_exists( 'edd_add_order_meta' ) ) {
edd_add_order_meta( $order_id, 'wpide_phone', $phone, true );
}
} );
add_action( 'edd_view_order_details_billing_after', function ( $order_id ) {
$phone = function_exists( 'edd_get_order_meta' ) ? edd_get_order_meta( $order_id, 'wpide_phone', true ) : '';
if ( $phone ) {
printf( '<div class="edd-order-phone" style="padding:12px"><strong>%1$s</strong> <a href="tel:%2$s">%3$s</a></div>', esc_html( 'Phone:' ), esc_attr( preg_replace( '/[^\d+]/', '', $phone ) ), esc_html( $phone ) );
}
} );
add_action( 'edd_add_email_tags', function () {
edd_add_email_tag( 'checkout_phone', 'The phone number entered at checkout', function ( $order_id ) {
return esc_html( (string) edd_get_order_meta( $order_id, 'wpide_phone', true ) );
}, 'Phone' );
} );
Notes
Set $wpide_edd_phone_required to false to make the field optional, and use {checkout_phone} in Downloads > Emails templates.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.