首页 > 解决方案 > 联系表格 7 注册表的功能

问题描述

我正在尝试将 WordPress Contact Form 7 用户注册的功能从一个表单修改为多个表单。

我尝试修改某人在线共享的代码,但我使用 php 但学习还不够好!提前感谢您的一些建议。

//  create Registration

function create_user_from_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 by checking the title
    if ( $cfdata->title() == 'Registration: Retailer') {
        $password = wp_generate_password( 12, false );
        $email = $formdata['email'];
        $name = $formdata['username'];
        // Construct a username from the user's name
        $username = strtolower(str_replace(' ', '', $name));
        $name_parts = explode(' ',$name);
        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' => reset($name_parts),
                'display_name' => $formdata['company'],
                'first_name' => $formdata['firstname'],
                'last_name' => $formdata['lastname'],
                'role' => 'customer'
            );
            $user_id = wp_insert_user( $userdata );
            if ( !is_wp_error($user_id) ) {
                // Email login details to user
                $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
                $message = "Hello! We have received your registration, Your login details are as follows:" . "\r\n";
                $message .= sprintf(__('Username: %s'), $username) . "\r\n";
                $message .= sprintf(__('Password: %s'), $password) . "\r\n";
                $message .= home_url() . "\r\n";
                wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
            }
        }
    }
    return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);

上面的代码会将用户注册到网站,但它只适用于一种形式。我有 3 种表格我想将其应用到。也许 elseif 用于查看标题。我对此太陌生,想学习。

标签: phpfunctioncontact-form-7

解决方案


推荐阅读