It can be used for pre-filled fields or custom functionality with filterning and updating values.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function wpm_custom_date_shortcode_handler($tag) { if ( ! is_array( $tag ) ) return ''; $name = $tag['name']; if ( empty( $name ) ) return ''; $next_week = date('Y-m-d', time() + (60*60*24*7)); $html = '<input type="text" name="' . $name . '" value="' . $next_week . '" />'; return $html; } wpcf7_add_shortcode( 'custom_date', 'wpm_custom_date_shortcode_handler', true ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function wpm_password_validation_filter( $result, $tag ) { $tag = new WPCF7_Shortcode( $tag ); if ( 'PASSWORD-CONFIRM' == $tag->name ) { $your_password = isset( $_POST['PASSWORD'] ) ? trim( $_POST['PASSWORD'] ) : ''; $your_password_confirm = isset( $_POST['PASSWORD-CONFIRM'] ) ? trim( $_POST['PASSWORD-CONFIRM'] ) : ''; if ( $your_password != $your_password_confirm ) { $result->invalidate( $tag, "Are you sure this is the correct password?" ); } } return $result; } add_filter( 'wpcf7_validate_text*', 'wpm_password_validation_filter', 20, 2 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function wpm_email_validation_filter( $result, $tag ) { $tag = new WPCF7_Shortcode( $tag ); if ( 'EMAIL-CONFIRM' == $tag->name ) { $your_email = isset( $_POST['EMAIL'] ) ? trim( $_POST['EMAIL'] ) : ''; $your_email_confirm = isset( $_POST['EMAIL-CONFIRM'] ) ? trim( $_POST['EMAIL-CONFIRM'] ) : ''; if ( $your_email != $your_email_confirm ) { $result->invalidate( $tag, "Are you sure this is the correct address?" ); } } if ( 'EMAIL' == $tag->name ) { $email = isset( $_POST['EMAIL'] ) ? trim( $_POST['EMAIL'] ) : ''; if ( email_exists( $email ) ) { $result->invalidate( $tag, "This email already exists, please choose another." ); } } return $result; } add_filter( 'wpcf7_validate_email*', 'wpm_email_validation_filter', 20, 2 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
function wpm_create_user_form_registration( $cfdata ) { if ( ! isset( $cfdata->posted_data ) && class_exists( 'WPCF7_Submission' ) ) { // Contact Form 7 version 3.9 removed $cfdata->posted_data and now // we have to retrieve it from an API $submission = WPCF7_Submission::get_instance(); if ( $submission ) { $formdata = $submission->get_posted_data(); } } elseif ( isset( $cfdata->posted_data ) ) { // For pre-3.9 versions of Contact Form 7 $formdata = $cfdata->posted_data; } else { // We can't retrieve the form data return $cfdata; } // Check this is the user registration form if ( $cfdata->title() == 'Registration' ) { $username = $formdata['EMAIL']; $email = $formdata['EMAIL']; $password = $formdata['PASSWORD']; $fname = $formdata['FNAME']; $lname = $formdata['LNAME']; if ( ! email_exists( $email ) ) { // Find an unused username $username_tocheck = $username; $i = 1; while ( username_exists( $username_tocheck ) ) { $username_tocheck = $username . $i ++; } $username = $username_tocheck; // Create the user $userdata = array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $email, 'nickname' => $fname . ' ' . $lname, 'display_name' => $fname . ' ' . $lname, 'first_name' => $fname, 'last_name' => $lname, 'role' => 'subscriber' ); $user_id = wp_insert_user( $userdata ); if ( ! is_wp_error( $user_id ) ) { wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id ); do_action( 'woocommerce_created_customer', $user_id ); } } } return $cfdata; } add_action( 'wpcf7_before_send_mail', 'wpm_create_user_form_registration', 1 ); |