【问题标题】:wordpress subscriber with contact form 7带有联系表 7 的 wordpress 订阅者
【发布时间】:2020-12-10 10:06:56
【问题描述】:

有谁知道联系表格 7 是否可以有一个复选框,当它被访问者激活时,这个访问者被注册为 wordpress 用户,作为订阅者?

在霍华德E的评论之后,问题变成了……

如何使用wpcf7_before_send_mail用表单的数据注册一个新用户?

类似这样,但我不知道它是否完全正确......

add_action( 'wpcf7_before_send_mail', 'register_user' );

function register_user($cf7) {
    $form_id = $cf7->id();
    
    if ($form_id == 300 || $form_id == 301 || $form_id == 302) {
        $submission = WPCF7_Submission :: get_instance();
    }
    if ($submission) {
        if (empty($posted_data)) { return; }
        
        $accept = $posted_data['acceptance-register-yes']; //acceptance check to be registered
        if (empty($accept)) { return; }
        
        $email = $posted_data['your-email'];
        $name1 = $posted_data['your-name'];
        $name2 = $posted_data['your-last-name'];
        $name = ''; //here will go function to delete spaces and generate user name from $name1 + $name2
        $pass = ''; //here will go function to random password
        
        function create_user($n,$p,$e){
            if (!username_exists($n)  && !email_exists($e)) {
                $user_id = wp_create_user($n, $p, $e);
                $user = new WP_User($user_id);
                $user->set_role( 'subscriber' );
            }
        }
        create_user($name,$pass,$email);        
    }   
}

非常感谢

【问题讨论】:

  • 当然这是可能的。您可以使用钩子wpcf7_before_send_mail - 但是,您的问题本身是题外话,您应该考虑修改它。
  • 至少有一个插件可用于此目的。 wordpress.org/plugins/frontend-registration-contact-form-7
  • 我认为前端注册插件不允许复选框注册

标签: wordpress forms contact-form-7 user-registration


【解决方案1】:

您可以像这样创建一个用户并连接到wpcf7_before_send_mail

function wp_create_custom_account(){
  
  // Get the WPCF7 Submission instance
  $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $posted_data = $submission->get_posted_data();

        // Get the posted variables
        $username = isset($posted_data['username'])?$posted_data['username']:'';
        $password = isset($posted_data['password'])?$posted_data['password']:'';
        $eml = isset($posted_data['eml'])?$posted_data['eml']:'';
        
        // This can be radio or checkbox. Adjust your code accordingly
        $radio = isset($posted_data['radio'][0])?$posted_data['radio'][0]:'';

        if ($radio) {
          $user = $username;
          $pass = $password;
          $email = eml;
          if ( !username_exists( $user )  && !email_exists( $email ) ) {
            $user_id = wp_create_user( $user, $pass, $email );
            $user = new WP_User( $user_id );
            $user->set_role( 'subscriber' );
          } 
        }
    } 
  
  
}
add_action('wpcf7_before_send_mail','wp_create_custom_account');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2015-11-24
    • 2012-09-29
    相关资源
    最近更新 更多