Guest User

Untitled

a guest
Oct 13th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <form id="login" class="ajax-auth" action="login" method="post">
  2. <p class="status"></p>
  3. <?php wp_nonce_field('ajax-login-nonce', 'security'); ?>
  4. <div class="form-group">
  5. <input id="username" placeholder="<?php _e('Username','my-theme'); ?>" type="text" class="required form-control" name="username">
  6. </div>
  7. <div class="form-group">
  8. <input id="password" placeholder="<?php _e('Password','my-theme'); ?>" type="password" class="required form-control" name="password">
  9. </div>
  10. <input class="btn btn-block btn-md btn-info" type="submit" value="<?php _e('Login','my-theme'); ?>">
  11. </form>
  12.  
  13. function ajax_auth_init(){
  14. global $theme_options;
  15. $userProfilePage=$theme_options['opt-user-select'];
  16.  
  17. wp_register_script('ajax-auth-script', get_template_directory_uri() . '/assets/js/ajax-logreg-script.js', array('jquery') );
  18. wp_enqueue_script('ajax-auth-script');
  19.  
  20. wp_localize_script( 'ajax-auth-script', 'ajax_auth_object', array(
  21. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  22. 'redirecturl' => get_page_link($userProfilePage),
  23. 'loadingmessage' => __('Please wait...','my-theme')
  24. ));
  25.  
  26. // Enable the user with no privileges to run ajax_login() in AJAX
  27. add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
  28. }
  29. // Execute the action only if the user isn't logged in
  30. if (!is_user_logged_in()) {
  31. add_action('init', 'ajax_auth_init');
  32. }
  33. function ajax_login(){
  34.  
  35. // First check the nonce, if it fails the function will break
  36. check_ajax_referer( 'ajax-login-nonce', 'security' );
  37.  
  38. // Nonce is checked, get the POST data and sign user on
  39. // Call auth_user_login
  40. auth_user_login($_POST['username'], $_POST['password'], __('Login','my-theme'));
  41.  
  42. die();
  43. }
  44. function auth_user_login($user_login, $password, $login){
  45. $info = array();
  46. $info['user_login'] = $user_login;
  47. $info['user_password'] = $password;
  48. $info['remember'] = true;
  49.  
  50. $user_signon = wp_signon( $info, false );
  51. if ( is_wp_error($user_signon) ){
  52. echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong password or username.','my-theme')));
  53. } else {
  54. wp_set_current_user($user_signon->ID);
  55. echo json_encode(array('loggedin'=>true, 'message'=> $login.__(' successful, redirecting...','my-theme')));
  56. }
  57.  
  58. die();
  59. }
  60.  
  61. jQuery(document).ready(function ($) {
  62.  
  63. // Perform AJAX login/register on form submit
  64. $('form#login, form#register').on('submit', function (e) {
  65. if (!$(this).valid()) return false;
  66. $('p.status', this).show().text(ajax_auth_object.loadingmessage);
  67. action = 'ajaxlogin';
  68. username = $('form#login #username').val();
  69. password = $('form#login #password').val();
  70. email = '';
  71. security = $('form#login #security').val();
  72. ctrl = $(this);
  73. $.ajax({
  74. type: 'POST',
  75. dataType: 'json',
  76. url: ajax_auth_object.ajaxurl,
  77. data: {
  78. 'action': action,
  79. 'username': username,
  80. 'password': password,
  81. 'email': email,
  82. 'security': security,
  83. },
  84. success: function (data) {
  85. $('p.status', ctrl).text(data.message);
  86. if (data.loggedin == true)
  87. document.location.href = ajax_auth_object.redirecturl;
  88. else if (ctrl == 'register')
  89. grecaptcha.reset();
  90. }
  91. });
  92. e.preventDefault();
  93. });
  94.  
  95. $(".logreg-page form#login").validate({
  96. ignore: ".ignore",
  97. rules: {
  98. username: {
  99. required: true,
  100. },
  101. password: {
  102. required: true,
  103. },
  104. },
  105. messages: {
  106. username: "Please enter an username.",
  107. password: "Please enter your password.",
  108. },
  109. });
  110. });
Add Comment
Please, Sign In to add comment