Skip to content

Instantly share code, notes, and snippets.

@Soullighter

Soullighter/cf7.php Secret

Created May 24, 2019
Embed
What would you like to do?
CF7 register user
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
if ( $cfdata->title() == 'Alumni članstvo_copy') {
$password = wp_generate_password( 12, false );
$name = $formdata['alumni-ime'];
$surname = $formdata['alumni-prezime'];
$email = $formdata['alumni-email'];
$email_show = $formdata['email-show-condition'];
$faculty = $formdata['alumni-faculty'];
$degree = $formdata['alumni-stepen'];
$faculty_sing_in_date = $formdata['alumni-sing-in-faculty-date'];
$faculty_sing_in_date_show = $formdata['sing-in-faculty-date-condition'];
$job_company = $formdata['alumni-job-company'];
$job_company_show = $formdata['job-company-condition'];
$job_position = $formdata['alumni-job-position'];
$job_position_show = $formdata['job-position-condition'];
$avatar = $formdata['alumni-profile-image'];
// 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' => $name,
'first_name' => reset($name_parts),
'last_name' => $surname,
'role' => 'alumni'
);
$user_id = wp_insert_user( $userdata );
if ( !is_wp_error($user_id) ) {
global $wpdb;
update_user_meta( $user_id, 'email_show_condition', $email_show );
update_user_meta( $user_id, 'alumni_faculty', $faculty );
update_user_meta( $user_id, 'alumni_stepen', $degree );
update_user_meta( $user_id, 'alumni_sing_in_faculty_date', $faculty_sing_in_date );
update_user_meta( $user_id, 'sing_in_faculty_date_condition', $faculty_sing_in_date_show );
update_user_meta( $user_id, 'alumni_job_company', $job_company );
update_user_meta( $user_id, 'job_company_condition', $job_company_show );
update_user_meta( $user_id, 'alumni_job_position', $job_position );
update_user_meta( $user_id, 'job_position_condition', $job_position_show );
update_user_meta( $user_id, 'alumni_profile_image', $img_id );
}
}
}
return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment