Ajax login WordPress
Sometime we need to login in site without reloading the page.so we simply use ajax for it. first we add login form in html than register a ajax path and than use jquery execute process. 1. Form Html using wp_footer hook # add ajax login form to page <?php add_action(“wp_footer”, “CustomLoginHtml”); function CustomLoginHtml() { if (!is_user_logged_in()) { $actual_link = (isset($_SERVER[“HTTPS”]) ? “https” : “http”) . “://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”; ob_start(); ?> <form id=”login” class=”buyback-ajax-auth” action=”login” method=”post”> <div class=”title-text”>Login</div> <p class=”status”></p> <?php echo wp_nonce_field( “ajax-login-nonce”, “security”, true, false ); ?> <label for=”username”>Username</label> <input id=”username” type=”text” class=”required” name=”username”> <label for=”password”>Password</label> <input id=”password” type=”password” class=”required” name=”password”> <div class=”cf”> <input class=”submit_button” type=”submit” value=”LOGIN”> </div> <a class=”login_close” href=””>×</a> <input id=”redirectto” type=”hidden” value=”‘.$actual_link.'”> </form> <?php echo ob_get_clean(); } } 2. Ajax Define <?php add_action(“wp_ajax_nopriv_ajaxlogin”, “ajax_login”); function ajax_login() { check_ajax_referer(“ajax-login-nonce”, “security”); auth_user_login($_POST[“username”], $_POST[“password”], “Login”); die(); } function auth_user_login($user_login, $password, $login) { $info = []; $info[“user_login”] = $user_login; $info[“user_password”] =