2013-06-26 49 views
1

作爲一個更大的項目的一部分獲取URL變量,我試圖所有登錄活動,地址如下發生:WordPress的:與重寫頁面(登錄頁面)

www.url.com/login

所以,一個鏈接應導致一種形式,將允許用戶重置其密碼將在下文中找到:

www.url.com/login?action=lostpassword

然而,wp-login.php沒有正確讀這篇文章,當我去上面的地址,我會得到正常的登錄表單。

這是我如何把它設置了:

add_action('init', 'add_virtual_page_template'); 
function add_virtual_page_template() 
{ 
    global $wp, $wp_rewrite; 

    $wp->add_query_var('template'); 

    add_rewrite_endpoint('login', EP_PERMALINK | EP_PAGES); 
    add_rewrite_rule('login/?', 'index.php?template=login', 'top'); 

    add_rewrite_rule('login/?', 'index.php?template=login', 'top'); 

    $wp_rewrite->flush_rules(); 
} 

add_action('template_redirect', 'add_virtual_page_redirect'); 
function add_virtual_page_redirect() 
{ 
    global $wp; 

    $queryvar = get_query_var('template'); 

    if ($queryvar && $queryvar == 'login') 
    { 
     include(site_url('wp-login.php')); 
     exit(); 
    } 

    if ($queryvar == 'mylogin') 
    { 
     include(get_stylesheet_directory() . '/page-login.php'); 
     exit(); 
    } 
} 

我缺少什麼?

回答

1

這是我如何處理這個擴建的Web應用程序工具包WordPress的(https://github.com/cferdinandi/web-app-starter-kit)時:

// LOGIN FORM SHORTCODE 

function wpwebapp_login() { 

    // Get current page URL 
    $url_current = @($_SERVER["HTTPS"] != 'on') ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"]; 
    $url_current .= ($_SERVER["SERVER_PORT"] !== 80) ? ":".$_SERVER["SERVER_PORT"] : ""; 
    $url_current .= $_SERVER["REQUEST_URI"]; 
    $url_clean = array_shift(explode('?', $url_current)); 
    $login_failed = $url_clean . '?login=failed'; 
    $signup_success = $url_clean . '?signup=success'; 
    $reset_success = $url_clean . '?password-reset=success'; 

    // Variables 
    $login_status = ''; 

    // If login failed 
    if ($url_current == $login_failed) { 
     $login_status = '<div class="alert alert-red">Invalid username or password. Please try again.</div>'; 
    } 

    // If password reset 
    if ($url_current == $signup_success) { 
     $login_status = '<div class="alert alert-green"><strong>Success!</strong> We just sent you an email with your password.</div>'; 
    } 

    // If password reset 
    if ($url_current == $reset_success) { 
     $login_status = '<div class="alert alert-green">Your password was successfully reset. We just emailed you a new one.</div>'; 
    } 

    // The login form 
    $form = 
     $login_status . 
     '<form name="login" id="wp_login_form" action="' . get_option('home') . '/wp-login.php" method="post"> 
      <div> 
       <label for="username">Username</label> 
       <input type="text" name="log" id="log" value="" tabindex="1" autofocus> 
      </div> 
      <div> 
       <label for="password">Password</label> 
       <input type="password" name="pwd" id="pwd" value="" tabindex="2"> 
      </div> 
      <div> 
       <label> 
        <input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" checked> 
        Remember Me 
       </label> 
      </div> 
      <div> 
       <button type="submit" name="wp-submit" id="wp-submit" tabindex="100" class="btn btn-blue">Log In</button><br> 
       <a href="' . $url_clean . 'password-reset/">Forgot your password?</a> 
       <input type="hidden" name="action" value="login"> 
       <input type="hidden" name="redirect_to" value="' . get_option('home') . '"> 
       <input type="hidden" name="testcookie" value="1"> 
      </div> 
     </form>'; 

    // Display the form 
    return $form; 
} 
add_shortcode('loginform', 'wpwebapp_login'); 



// FAILED LOGIN REDIRECT 

add_action('login_redirect', 'redirect_login', 10, 3); 
function redirect_login($redirect_to, $url, $user) { 

    // URL Variables 
    $referrer = $_SERVER['HTTP_REFERER']; 
    $url_clean = array_shift(explode('?', $referrer)); 
    $login_failed = $url_clean . '?login=failed'; 

    // If the post submission is a valid page that's not the backend login screen 
    if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')) { 
     // If the password is empty... 
     if($user->errors['empty_password']){ 
      wp_redirect($login_failed); 
     } 
     // If the username is empty... 
     else if($user->errors['empty_username']){ 
      wp_redirect($login_failed); 
     } 
     // If the username is invalid... 
     else if($user->errors['invalid_username']){ 
      wp_redirect($login_failed); 
     } 
     // If the password is incorrect... 
     else if($user->errors['incorrect_password']){ 
      wp_redirect($login_failed); 
     } 
     // Catch all for all other issues 
     else { 
       wp_redirect(get_option('home')); 
     } 
     exit; 
    } 

    // Prevents page from hanging when redirected from backend 
    if (!empty($referrer) && (strstr($referrer,'wp-login') || strstr($referrer,'wp-admin'))) { 
      wp_redirect(get_option('home')); 
      exit; 
    } 

} 



// BLOCK BACKEND ACCESS FOR NON-ADMINS 

add_action('init', 'blockusers_init'); 
function blockusers_init() { 
    // If accessing the admin panel and not an admin 
    if (is_admin() && !current_user_can('level_10')) { 
     // Redirect to the homepage 
     wp_redirect(home_url()); 
     exit; 
    } 
} 

採用這種設置,你曾經被重定向到後端(不幸的副作用阻止用戶:如果您未登錄,則需要先從前端登錄)。您可以根據情況等添加自定義錯誤消息。只需使用[loginform]簡碼即可將其添加到頁面中。

用於Web Apps工具包的WordPress具有類似於密碼重置,註冊表單等功能,因此您可能需要檢查它。去年,我在一個我正在研究的項目中經歷了同樣的學習過程。