Halloween Sale 15% off Ends in See the plans
PHP

"Request a Call Back" Form Shortcode

Adds a short [wpide_callback_form] form where visitors leave their name, phone number and the best time to call, and emails the request to you.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_callback = array(
    'to'    => '', // Recipient; empty = admin email.
    'slots' => array( 'Morning (9–12)', 'Afternoon (12–17)', 'Evening (17–19)', 'Any time' ),
);

add_action( 'template_redirect', function () use ( $wpide_callback ) {
    if ( empty( $_POST['wpide_cb_nonce'] ) ) {
        return;
    }

    $back = remove_query_arg( 'wpide_cb', wp_get_referer() ? wp_get_referer() : home_url( '/' ) );
    $done = function ( $status ) use ( $back ) {
        wp_safe_redirect( add_query_arg( 'wpide_cb', $status, $back ) . '#wpide-callback' );
        exit;
    };

    if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wpide_cb_nonce'] ) ), 'wpide_cb' ) ) {
        $done( 'expired' );
    }
    if ( ! empty( $_POST['wpide_cb_hp'] ) ) {
        $done( 'sent' );
    }

    $ip  = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
    $key = 'wpide_cb_' . md5( wp_salt() . $ip );
    if ( (int) get_transient( $key ) >= 3 ) {
        $done( 'limit' );
    }

    $name  = sanitize_text_field( wp_unslash( isset( $_POST['wpide_cb_name'] ) ? $_POST['wpide_cb_name'] : '' ) );
    $phone = preg_replace( '/[^0-9+()\s.\-]/', '', (string) wp_unslash( isset( $_POST['wpide_cb_phone'] ) ? $_POST['wpide_cb_phone'] : '' ) );
    $slot  = sanitize_text_field( wp_unslash( isset( $_POST['wpide_cb_slot'] ) ? $_POST['wpide_cb_slot'] : '' ) );
    $slot  = in_array( $slot, $wpide_callback['slots'], true ) ? $slot : 'Any time';

    if ( '' === $name || strlen( preg_replace( '/\D/', '', $phone ) ) < 7 ) {
        $done( 'invalid' );
    }

    $to   = is_email( $wpide_callback['to'] ) ? $wpide_callback['to'] : get_option( 'admin_email' );
    $sent = wp_mail( $to, 'Call back request: ' . $name, "Name: $name\nPhone: $phone\nBest time: $slot\nPage: $back" );

    set_transient( $key, (int) get_transient( $key ) + 1, HOUR_IN_SECONDS );
    $done( $sent ? 'sent' : 'error' );
} );

add_shortcode( 'wpide_callback_form', function () use ( $wpide_callback ) {
    $messages = array(
        'sent'    => 'Thanks! We will call you back at the time you chose.',
        'invalid' => 'Please enter your name and a valid phone number.',
        'limit'   => 'You have already sent a request. We will be in touch soon.',
        'expired' => 'The form expired. Please try again.',
        'error'   => 'Sorry, the request could not be sent. Please call us instead.',
    );
    $status = isset( $_GET['wpide_cb'] ) ? sanitize_key( $_GET['wpide_cb'] ) : '';

    $html  = '<div id="wpide-callback" class="wpide-form wpide-callback-form">';
    $html .= isset( $messages[ $status ] ) ? '<p class="wpide-form-notice" role="status">' . esc_html( $messages[ $status ] ) . '</p>' : '';
    $html .= '<form method="post" action="">';
    $html .= '<p><label for="wpide-cb-name">Name</label><br><input id="wpide-cb-name" name="wpide_cb_name" required autocomplete="name"></p>';
    $html .= '<p><label for="wpide-cb-phone">Phone number</label><br><input type="tel" id="wpide-cb-phone" name="wpide_cb_phone" required autocomplete="tel"></p>';
    $html .= '<p><label for="wpide-cb-slot">Best time to call</label><br><select id="wpide-cb-slot" name="wpide_cb_slot">';
    foreach ( $wpide_callback['slots'] as $slot ) {
        $html .= '<option>' . esc_html( $slot ) . '</option>';
    }
    $html .= '</select></p><p style="position:absolute;left:-9999px;" aria-hidden="true"><input name="wpide_cb_hp" tabindex="-1" autocomplete="off"></p>';
    $html .= wp_nonce_field( 'wpide_cb', 'wpide_cb_nonce', false, false );
    $html .= '<p><button type="submit">Call me back</button></p></form></div>';

    return $html;
} );

Notes

Change the time slots and recipient at the top; works well in a sidebar or at the end of service pages.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.