Created
June 6, 2019 15:27
-
-
Save himanshuahuja96/4346db6d79e2e435d7a6cd355bfef704 to your computer and use it in GitHub Desktop.
Helps in automating members signup with the custom forms based on convertpro or even php forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//split name into first and last | |
function split_name($name) { | |
$name = trim($name); | |
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name); | |
$first_name = trim( preg_replace('#'.$last_name.'#', '', $name ) ); | |
return array($first_name, $last_name); | |
} | |
add_action("cp_after_subscribe","add_my_user",10,2); | |
function add_my_user( $email, $service ){ | |
$posteddata = ConvertPlugHelper::get_post_data(); | |
global $response; | |
if(!username_exists( $user_name )) | |
{ | |
if(!email_exists($email)){ | |
$name = $posteddata["param"]["textfield_8062"]; | |
$name_splitted = split_name($posteddata["param"]["textfield_8062"]); | |
$password = $posteddata["param"]["textarea_8481"]; | |
$user_email = $email; | |
$user_id = wp_create_user( $user_email, $password, $user_email ); | |
if($user_id){ | |
$response['user_id'] = $user_id; | |
$user_info = get_userdata($user_id); | |
// create md5 code to verify later | |
$code = md5(time()); | |
// make it into a code to send it to user via email | |
$string = array('id'=>$user_id, 'code'=>$code , 'password' => $password , 'email' => $user_email); | |
// create the activation code and activation status | |
update_user_meta($user_id, 'account_activated', 0); | |
update_user_meta($user_id, 'activation_code', $code); | |
update_user_meta($user_id , 'display_name' , $name); | |
update_user_meta($user_id , 'first_name' , $name_splitted[0]); | |
update_user_meta($user_id , 'last_name' , $name_splitted[1]); | |
// create the url | |
$url = get_site_url(). '/verify/?act=' .base64_encode( serialize($string)); | |
// basically we will edit here to make this nicer | |
$html = 'Please click the following links <br/><br/> <a href="'.$url.'">'.$url.'</a>'; | |
// send an email out to user | |
wp_mail( $user_info->user_email, __('Verify Your Account to Access the services','text-domain') , $html); | |
} | |
} | |
else{ | |
$response['error'] = true; | |
$response['error_content'] = 'email id already exists'; | |
return $response; | |
} | |
} | |
else{ | |
$response['error'] = true; | |
$response['error_content'] = 'username already used'; | |
return $response; | |
} | |
} | |
add_action( 'init', 'verify_user_code' ); | |
function verify_user_code(){ | |
if(isset($_GET['act'])){ | |
$data = unserialize(base64_decode($_GET['act'])); | |
$code = get_user_meta($data['id'], 'activation_code', true); | |
// verify whether the code given is the same as ours | |
if($code == $data['code']){ | |
// update the user meta | |
update_user_meta($data['id'], 'is_activated', 1); | |
$user = new WP_User($data['id']); | |
$user->set_role('custom_role1'); | |
$creds = array( | |
'user_login' => $data['email'], | |
'user_password' => $data['password'], | |
'remember' => true | |
); | |
//var_dump($creds); | |
$user = wp_signon( $creds, false ); | |
if($user){ | |
$location = pmpro_url( "checkout", "?level=" . 12); | |
wp_safe_redirect($location); | |
//var_dump($location); | |
exit; | |
} | |
else if( is_wp_error( $user ) ) { | |
echo $user->get_error_message(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment