Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created December 15, 2014 11:20
Show Gist options
  • Save vishalbasnet23/6caf4956c0bdc63c8532 to your computer and use it in GitHub Desktop.
Save vishalbasnet23/6caf4956c0bdc63c8532 to your computer and use it in GitHub Desktop.
Custom Activation Link on Sign Up WordPress
Step by step guide.
First Phase:
1.First create a html form with first name, last name and email field for signing up process.( Remember the action of the form it has to be the page slug of email confirmation page that you will be creating at in next phase. )
Second Phase :
1.Create a Page ( Remember the slug has to be the same of that of action of the form of phase one ) and assign the template template-confirmation-mail.php
2.This page is responsible for generating unqiue hash key and sending that hash key along with the user's new account credentials to their email.
3.Remember the link of the activation_link has to the link to the email-verification template, which we will be creating in next phase.
Third Phase :
1. Create a page and assign it to Email Verification Template.( Remember this template has to be assigned to the page that you sent in the mail in previous step )
2. This page is responsilble matching that unqiue key sent on the email once new user visit that page.
Fourth Phase:
1. Write a function to block not activated user on functions.php
/***************************************************************************************************************************/
/* Please Feel free to contact me if you have any confusion. :) */
/**************************************************************************************************************************/
<form name="signup-declaration" method="POST" id="signatory-sign-up-form" action="<?php echo get_permalink(get_page_by_path('confirm-email-page'));?>">
<div class="row">
<div class="col-sm-6">
<input type="text" name="fname" class="form-control" placeholder="First Name *">
</div>
<div class="col-sm-6">
<input type="text" name="lname" class="form-control" placeholder="Last Name *">
</div>
</div><!--row ends-->
<div class="row">
<div class="col-sm-6">
<input id="email" type="email" name="email" class="form-control" placeholder="Email *">
</div>
<div class="col-sm-6">
<input type="email" name="c_email" class="form-control" placeholder="Confirm Email *">
</div>
<div class="col-sm-6">
<input type="submit" name="new_user_submit" value="sign-up">
</div><!--row ends-->
</div>
</form>
<?php add_filter('wp_authenticate_user', 'check_user_activation_status', 10, 2);
function check_user_activation_status($user) {
if ( get_user_meta($user->ID ,'wp_user_level',true) != 10 ) {
if( get_user_meta($user->ID, 'activated', true) == 'true' ) {
return $user;
}
} else {
return $user;
}
return new WP_Error('Account Not Active...');
}
?>
<?php
/* Template Name: Confirmation Link Page */
get_header();?>
<div class="inner-page-container">
<div class="container">
<div class="row">
<div class="col-sm-12 thankyou-signup">
<h1 class="blue-title">Almost Finished!</h1>
<div class="page-desp">
<?php
if( isset($_POST['new_user_submit']) ) :
$new_user_first_name = stripcslashes( $_POST['fname']);
$new_user_last_name = stripcslashes( $_POST['lname']);
$new_user_email = stripcslashes($_POST['email']);
$confirmaion_hash_key = md5( rand(0,1000) );
$new_user_password = md5(rand(1000,5000));
$user_data = array(
'user_login' => $new_user_email,
'user_email' => $new_user_email,
'first_name' => $new_user_first_name,
'last_name' => $new_user_last_name,
'role' => 'subscriber'
);
$new_user_id = wp_insert_user($user_data);
if (!is_wp_error($new_user_id)) {
$activation_link = get_permalink(get_page_by_path('email-verification')).'?email='.$new_user_email.'&hash='.$confirmaion_hash_key;
$email_content = "A mail has been sent to your email address. Please follow the link to activate your account";
update_user_meta($new_user_id,'activated','false');
$to = $new_user_email; // Send email to our user
$subject = 'Signup Verification'; // Give the email a subject
$message = 'Your login credential';
$message .= 'Username: '.$new_user_email.'<br />
Password: '.$new_user_password.'<br />
Please click on the following link or copy the link and paste it to your browser to activate your profile and Sign in.'
.$activation_link.';
// Our message above including the link
$headers = "From: " Your Name .<Your Email>\r\n";
$headers .= "Reply-To: "Your Email"\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7BIT";
wp_mail($to, $subject, $message, $headers); // Send our email
} else {
if (isset($new_user_id->errors['empty_user_login'])) {
$notice_key = 'User Name and Email are mandatory';
echo $notice_key;
} elseif (isset($new_user_id->errors['existing_user_login'])) {
echo'<p>User name already exixts.</p>';
} else {
echo'<p>Error Occured please fill up the sign up form carefully.</p>';
}
}
else:
echo 'You have not submitted the form';
endif;
</div>
</div><!--container ends-->
</div><!--inner-page-container ends-->
<?php get_footer();?>
<?php
/* Template Name: Email Verification */
get_header(); ?>
<div class="inner-page-container">
<div class="container">
<div class="row">
<div class="col-sm-12 thankyou-signup">
<h1 class="blue-title">Finished!</h1>
<div class="page-desp">
<?php if( isset($_GET['email']) && isset($_GET['hash']) ){
// Verify data
$email = ($_GET['email']); // Set email variable
$hash = ($_GET['hash']); // Set hash variable
$match = 0;
$matched_user_id = 0;
$all_subscriber = get_users( 'role=subscriber' );
foreach ( $all_subscriber as $subscriber ) {
$subscriber_id = $$subscriber->ID;
$user_hash = get_user_meta( $subscriber_id, 'hash', true );
if( $user_hash == $_GET['hash'] ) {
$match = 1;
$matched_user_id = $subscriber_id;
}
}
if($match > 0){
update_user_meta($matched_user_id,'activated','true');
echo '<p>Your account has been activated, you can now login with password we sent you in the mail</p>';
}else{
// No match -> invalid url or account has already been activated.
echo '<p>The url is either invalid or you already have activated your account.</p>';
}
}else{
// Invalid approach
echo '<p>Invalid approach, please use the link that has been send to your email.</p>';
}
</div>
</div><!--container ends-->
</div><!--inner-page-container ends-->
<?php get_footer(); ?>
@rjtkatoch007
Copy link

Fatal error: Uncaught Error: Object of class WP_User could not be converted to string. line no 18 $subscriber_id = $$subscriber->ID;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment