Created
January 23, 2017 09:07
-
-
Save dasbairagya/adcdc6e5f06f82ef3687277a979b8aa4 to your computer and use it in GitHub Desktop.
Login/Register for wordpress
This file contains hidden or 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
| <?php | |
| /***After Successful login redirects**/ | |
| //function redirect_user_on_role() | |
| //{ | |
| // global $current_user; | |
| // get_currentuserinfo(); | |
| // | |
| // //If login user role is Subscriber | |
| // if ($current_user->user_level == 0) | |
| // { | |
| // wp_redirect( home_url() ); exit; | |
| // } | |
| // | |
| // //If login user role is Contributor | |
| // if ($current_user->user_level > 1) | |
| // { | |
| // wp_redirect( home_url() ); exit; | |
| // } | |
| // | |
| // //If login user role is Editor | |
| // if ($current_user->user_level > 8) | |
| // { | |
| // wp_redirect( home_url() ); exit; | |
| // } | |
| //} | |
| //add_action('admin_init', 'redirect_user_on_role'); | |
| /*add action for member registration.............start..............*/ | |
| add_action( 'wp_ajax_create_user', 'it_create_user' ); | |
| add_action( 'wp_ajax_nopriv_create_user', 'it_create_user' ); | |
| function it_create_user() { | |
| global $wpdb; | |
| $from_name = get_bloginfo('name'); | |
| $from_email = get_bloginfo('admin_email'); | |
| // Handle request then generate response using WP_Ajax_Response | |
| $name = $_POST['name']; | |
| $email = $_POST['email']; | |
| $phone = $_POST['phone']; | |
| $password = $_POST['password']; | |
| //Add a zero at begining of phone number | |
| if(email_exists($email)){ | |
| $save_value = 'email_exists'; | |
| } | |
| else if(username_exists($name)){ | |
| $save_value = 'username_exists'; | |
| } | |
| else { | |
| // $password = wp_generate_password( 10, true, true ); | |
| $user_id = wp_create_user($name, $password, $email); | |
| $user_name= sanitize_title_with_dashes($name); | |
| add_user_meta($user_id, 'name', $name); | |
| add_user_meta($user_id, 'email', $email); | |
| add_user_meta($user_id, 'phone', $phone); | |
| add_post_meta($user_id, 'password', $password); | |
| if($user_id){ | |
| /////////////// first mail to user - start ////////////////////////////////// | |
| $fullname = ucwords(strtolower($user_name)); | |
| $subject = "Welcome to ".$from_name; | |
| $message = '<p>Dear '. ucwords(strtolower($email)).',</p><p></p><p>You have successfully created an account to our Website.<br>Your User Name: '.$email.'<br> Your password is : '.$password.'<br>'; | |
| //Headers | |
| $headers = 'MIME-Version: 1.0' . "\r\n"; | |
| $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n"; | |
| if(!empty($from_email) && filter_var($from_email,FILTER_VALIDATE_EMAIL))//Validating From | |
| $headers .= "From: ".$from_name." <".$from_email."> \r\n"; | |
| $reply_email = $from_email; | |
| if($reply_email){ | |
| $headers .= "Reply-To: {$reply_email}\r\n"; | |
| $headers .= "Return-Path: {$from_name}\r\n"; | |
| } | |
| wp_mail($email, $subject, $message , $headers); | |
| /////////////// first mail to user - end ////////////////////////////////// | |
| /////////////// Mail to admin - start ////////////////////////////////// | |
| $subject = $from_name."- New customer registration request"; | |
| $message = '<p>Dear Admin,</p><p>'.$fullname.' is registered in our website!</p><br>User Details are:<br> Name: '.$name.'<br>Phone no:'.$phone.'<br>Email: '.$email.'<br>' | |
| . '<p>Best Wishes,<br>Team '.$from_name; | |
| wp_mail($from_email, $subject, $message , $headers); | |
| /////////////// Mail to admin - end ////////////////////////////////// | |
| } | |
| $save_value = "Registration Successful"; | |
| } | |
| echo $save_value; | |
| //echo "success==============".$user_id; | |
| die; | |
| } | |
| /** Plugin Name: (#90328) Login with E-Mail address */ | |
| function login_with_email_address( &$username ) { | |
| $user = get_user_by( 'email', $username ); | |
| if ( !empty( $user->user_login ) ) | |
| $username = $user->user_login; | |
| return $username; | |
| } | |
| add_action( 'wp_authenticate','login_with_email_address' ); | |
| /*** | |
| LOGIN HERE | |
| */ | |
| //function custom_login() { | |
| // echo header("Location: " . get_bloginfo( 'url' ) . "/register"); | |
| //} | |
| // | |
| //add_action('login_head', 'custom_login'); | |
| // | |
| //function login_link_url( $url ) { | |
| // $url = get_bloginfo( 'url' ) . "/register"; | |
| // return $url; | |
| //} | |
| //add_filter( 'login_url', 'login_link_url', 10, 2 ); | |
| // | |
| //function taxonomy_checklist_checked_ontop_filter ($args) { $args['checked_ontop'] = false; return $args; } add_filter('wp_terms_checklist_args','taxonomy_checklist_checked_ontop_filter'); | |
| ?> |
This file contains hidden or 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
| <?php | |
| /** | |
| * Template Name: Login | |
| * Created by PhpStorm. | |
| * User: VIcky | |
| * Date: 9/30/2016 | |
| * Time: 2:01 AM | |
| */ | |
| if($_POST) { | |
| global $wpdb; | |
| //We shall SQL escape all inputs | |
| $username = $wpdb->escape($_REQUEST['username']); | |
| $password = $wpdb->escape($_REQUEST['password']); | |
| $remember = $wpdb->escape($_REQUEST['rememberme']); | |
| if($remember) $remember = "true"; | |
| else $remember = "false"; | |
| $login_data = array(); | |
| $login_data['user_login'] = $username; | |
| $login_data['user_password'] = $password; | |
| $login_data['remember'] = $remember; | |
| $user_verify = wp_signon( $login_data, false ); | |
| if ( is_wp_error($user_verify) ) | |
| { | |
| header("Location: " . home_url() . "/register?x=Wrong User Name Or Password.."); | |
| // Note, I have created a page called "Error" that is a child of the login page to handle errors. This can be anything, but it seemed a good way to me to handle errors. | |
| } else { | |
| echo "<script type='text/javascript'>window.location='". home_url() ."'</script>"; | |
| exit(); | |
| } | |
| } else { | |
| // No login details entered - you should probably add some more user feedback here, but this does the bare minimum | |
| echo "Invalid login details"; | |
| } | |
| ?> |
This file contains hidden or 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
| <?php | |
| /** | |
| * Template Name: Register | |
| * Created by PhpStorm. | |
| * User: VIcky | |
| * Date: 9/29/2016 | |
| * Time: 9:59 PM | |
| */ | |
| get_header(); | |
| $x=$_GET['x']; | |
| ?> | |
| <div class="container"> | |
| <div class="row"> | |
| <div class="custom-margin"> | |
| <div class="col-md-6 panel panel-danger" style="margin-top: 20px;"> | |
| <div class="panel-heading"> | |
| <h3 class="panel-title">Registration Here</h3> | |
| </div> | |
| <div class="panel-body"> | |
| <form id="signup-form" method="post" class="s-form wow zoomInUp" data-wow-delay="0.5s"> | |
| <div class="form-group col-md-12"> | |
| <label class="control-label" for="name">Enter Name: </label> | |
| <input type="text" placeholder="FULL NAME" value="" name="name" id="name" class="form-control" /> | |
| </div> | |
| <div class="form-group col-md-12"> | |
| <label class=" control-label" for="name">Email Address: </label> | |
| <input type="email" placeholder="EMAIL ID" value="" name="email" id="email" class="form-control" /> | |
| </div> | |
| <div class="form-group col-md-12"> | |
| <label class="control-label" for="name">Phone Number: </label> | |
| <input type="number" placeholder="PHONE NO" value="" name="phone" id="phone" class="form-control" /> | |
| </div> | |
| <div class="form-group col-md-12"> | |
| <label class=" control-label" for="name">Enter Password:</label> | |
| <input type="password" placeholder="PASSWORD" value="" name="password" id="password" class="form-control" /> | |
| </div> | |
| <input type="button" name="btn_submit" class="btn btn-danger" id="btn_submit1" value="Submit" style="float:right;margin-right: 17px;background-color:#ee8323;border-color:#ee8323;border-radius:0px;"> | |
| </form> | |
| </div> | |
| </div> | |
| <div class="col-md-6 panel panel-danger" style=" margin-top: 20px;"> | |
| <div class="panel-heading"> | |
| <h3 class="panel-title">Login Here</h3> | |
| </div> | |
| <p style="color:red;margin-left:10px;margin-top:10px"><?php echo $x; ?><p> | |
| <form id="login" class="s-form wow zoomInUp" name="form" action="<?php echo home_url(); ?>/login/" method="post"> | |
| <div class="form-group col-md-12"> | |
| <label class="control-label" for="name">Enter Email: </label> | |
| <input type="text" placeholder="Enter Username" value="" name="username" id="username" class="form-control" required/> | |
| </div> | |
| <div class="form-group col-md-12"> | |
| <label class="control-label" for="name">Enter Password: </label> | |
| <input type="password" placeholder="Password" value="" name="password" id="password" class="form-control" required/> | |
| </div> | |
| <input id="submit" class="btn btn btn-danger" type="submit" name="submit" value="Login" style="float:right;margin-right: 17px;margin-bottom: 10px;background-color:#ee8323;border-color:#ee8323;border-radius:0px;"> | |
| </form> | |
| <!-- <label for="chk" style="color:#026466">--> | |
| <p style="float:right;margin-right:10px;"><input type="checkbox" id="chk1" style="height:12px;margin-top:10px;width:20px;"><span style="margin-left:5px;">Forgot Password?</span></p> | |
| <div class="col-md-12" id="list1"> | |
| <form name="lostpasswordform" id="lostpasswordform" action="<?php echo wp_lostpassword_url(); ?>" method="post"> | |
| <p> | |
| <label class="control-label" for="name" style="color:#888;">Username or E-mail:<br> </label> | |
| <input type="text" name="user_login" id="user_login" class="form-control" value="" placeholder="Username or E-mail" required> | |
| </p> | |
| <input type="hidden" name="redirect_to" value="<?php echo $redirect ?>"> | |
| <input type="submit" name="wp-submit" id="wp-submit" class="btn btn btn-danger" value="Get New Password" style="float:right;margin-bottom: 10px;background-color:#ee8323;border-color:#ee8323;border-radius:0px;"> | |
| </form> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php get_footer(); ?> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
| <script> | |
| $(function () { | |
| // Get the form fields and hidden div | |
| var checkbox = $("#chk1"); | |
| var hidden = $("#list1"); | |
| hidden.hide(); | |
| checkbox.change(function () { | |
| if (checkbox.is(':checked')) { | |
| hidden.show(); | |
| } else { | |
| hidden.hide(); | |
| // $("#list").val(""); | |
| } | |
| }); | |
| }); | |
| </script> | |
| <script type="text/javascript"> | |
| var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>"; | |
| </script> | |
| <script type="text/javascript"> | |
| $(function() { | |
| $('#btn_submit1').click(function() { | |
| var name = $('#name').val(); | |
| var email = $('#email').val(); | |
| var phone = $('#phone').val(); | |
| var password = $('#password').val(); | |
| if (name == "") { | |
| swal(" Full Name is a required field"); | |
| return false; | |
| } | |
| if (email == "") { | |
| swal("Email is a required field"); | |
| return false; | |
| } | |
| if (phone == "") { | |
| swal("Phone is a required field"); | |
| return false; | |
| } | |
| if (password == "") { | |
| swal("Password is a required field"); | |
| return false; | |
| } | |
| var data = { | |
| 'action': 'create_user', | |
| 'name': name, | |
| 'email': email, | |
| 'phone': phone, | |
| 'password': password | |
| }; | |
| $.ajax({ | |
| type: "POST", | |
| url: ajaxurl, | |
| data: data, | |
| success: function (response) { | |
| alert(response); | |
| $("#signup-form")[0].reset(); | |
| }, | |
| error: function (response) { | |
| alert(response); | |
| $("#signup-form")[0].reset() | |
| }, | |
| }); | |
| }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment